Re: [R] replace values?

From: Matthias Braeunig <mb.atelier_at_web.de>
Date: Sun 02 Jul 2006 - 20:54:43 EST

# 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!



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Mon Jul 03 06:38:13 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Mon 03 Jul 2006 - 08:16:04 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.