[R] replace values?
Matthias Braeunig
mb.atelier at web.de
Sun Jul 2 12:54:43 CEST 2006
# reproducing your example
xx<-"x y z
+ 1 2 3
+ 2 3 1
+ 3 2 1
+ 1 1 3
+ 2 1 2
+ 3 2 3
+ 2 1 1"
# you did not tell us the class of your data, assuming data.frame
df<-read.table(textConnection(xx),header=T,colClasses="factor")
# a clean way to do what you want is using factors with ?levels
# (note that data has already been read as factor)
levels(df$x)<-c("a","b","c","d")
levels(df$y)<-c("b","a","c","d")
levels(df$z)<-c("d","c","b","a")
subset(df,x=="a")
x y z
1 a a b
4 a b b
subset(df,x=="a"&y=="a")
x y z
1 a a b
HTH, m
zhijie zhang wrote:
> Dear friends,
> i have a dataset like this:
> x y z
> 1 2 3
> 2 3 1
> 3 2 1
> 1 1 3
> 2 1 2
> 3 2 3
> 2 1 1
> I want to replace x with the following values:1<-a,2<-b,3<-c,4<-d;
> replace y with the following values:1<-b,2<-a,3<-c,4<-d;
> replace z with the following values:1<-d,2<-c,3<-b,4<-a;
> Finally,select two subsets:
> 1. if x='a';
> 2.x='a' and y='a';
> thanks very much!
More information about the R-help
mailing list