[R] replace values?

Jonathan Baron baron at psych.upenn.edu
Sun Jul 2 12:12:43 CEST 2006


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



More information about the R-help mailing list