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! -- Kind Regards, Zhi Jie,Zhang ,PHD Department of Epidemiology School of Public Health Fudan University Tel:86-21-54237149 [[alternative HTML version deleted]]
On 07/02/06 12:39, 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;Here's one way. Call your dataset M, and assume it is a data.frame. This method of replacement works best when you are replacing consecutive integers, as you are. Note that X[1] is "a", X[2] is "b" and so on. X <- c("a","b","c","d") Y <- c("b","a","c","d") Z <- c("d","c","b","a") M$x <- X[M$x] M$y <- Y[M$y] M$z <- Z[M$z]> Finally,select two subsets: > 1. if x='a'; > 2.x='a' and y='a';M[M$x=="a",] M[M$x=="a" & M$y=="a",] The subsets will be rows. I'm not sure that's what you mean. Jon -- Jonathan Baron, Professor of Psychology, University of Pennsylvania Home page: http://www.sas.upenn.edu/~baron
# 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!
Maybe Matching Threads
- Generating series of distributions with the same skewness and different kurtosis or with same kurtosis and different skewness?
- How to add legend for image()?
- handle dates in R?
- Is there any good tools to facilitate us to create R functions?
- set the bahavior that R deal with missing values?