Re: [R] Data replacement

2010-01-05 Thread Dieter Menne



Lisa wrote:
 
 I have a dataset that looks like this: 
 
 data
  idcode1code2 
 1 114 
 2 123 
 3 244 
 ..
 
 I want to change some numbers in the columns of “code1” and “code2” based
 on “indx” as below
 
 indx
 [[1]]
 code 
 1  1 
 2  3 
 3  4 
 For example,  for the first ten records (rows) of my dataset, I want to
 change 2 to 3, 3 to 4, 4 to 6, and 5 to 8 in both “code1” and “code2”,
 while for the last ten records, I want to change 3 to 4 and 4 to 6.
 
 

You might check for recode, for example in package car, or for
transform. You could also do it the quick and dirty way, good to learn
indexing. Be careful if you have NA in your data, or data out of the recode
range.

Dieter


data = data.frame(code1=sample(1:5,10,TRUE),code2=sample(1:5,10,TRUE))
data =
rbind(data,data.frame(code1=sample(1:4,10,TRUE),code2=sample(1:4,10,TRUE)))

# The recode table as  in your example
#indx = list(data.frame(code=c(1,3,4,6,8)),data.frame(code=c(1,2,4,6)))

#easier to read
recode1 = c(1,3,4,6,8)
recode2 = c(1,2,4,6)

data$code1T[1:10] = recode1[data$code1[1:10]]
data$code2T[1:10] = recode1[data$code2[1:10]]

data$code1T[11:20] = recode2[data$code1[11:20]]
data$code2T[11:20] = recode2[data$code2[11:20]]




-- 
View this message in context: 
http://n4.nabble.com/Data-replacement-tp999060p999176.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Data replacement

2010-01-05 Thread Lisa

Thank you for your kind help. Your R script works well.

Lisa



Dieter Menne wrote:
 
 
 
 Lisa wrote:
 
 I have a dataset that looks like this: 
 
 data
  idcode1code2 
 1 114 
 2 123 
 3 244 
 ..
 
 I want to change some numbers in the columns of “code1” and “code2” based
 on “indx” as below
 
 indx
 [[1]]
 code 
 1  1 
 2  3 
 3  4 
 For example,  for the first ten records (rows) of my dataset, I want to
 change 2 to 3, 3 to 4, 4 to 6, and 5 to 8 in both “code1” and “code2”,
 while for the last ten records, I want to change 3 to 4 and 4 to 6.
 
 
 
 You might check for recode, for example in package car, or for
 transform. You could also do it the quick and dirty way, good to learn
 indexing. Be careful if you have NA in your data, or data out of the
 recode range.
 
 Dieter
 
 
 data = data.frame(code1=sample(1:5,10,TRUE),code2=sample(1:5,10,TRUE))
 data =
 rbind(data,data.frame(code1=sample(1:4,10,TRUE),code2=sample(1:4,10,TRUE)))
 
 # The recode table as  in your example
 #indx = list(data.frame(code=c(1,3,4,6,8)),data.frame(code=c(1,2,4,6)))
 
 #easier to read
 recode1 = c(1,3,4,6,8)
 recode2 = c(1,2,4,6)
 
 data$code1T[1:10] = recode1[data$code1[1:10]]
 data$code2T[1:10] = recode1[data$code2[1:10]]
 
 data$code1T[11:20] = recode2[data$code1[11:20]]
 data$code2T[11:20] = recode2[data$code2[11:20]]
 
 
 
 
 

-- 
View this message in context: 
http://n4.nabble.com/Data-replacement-tp999060p999342.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.