Monday, 19 August 2013

Recoding over multiple data frames in R

Recoding over multiple data frames in R

I'm a bit stuck on what I suspect is an easy enough problem. I have
multiple different data sets that I have loaded into R, all of which have
different numbers of observations, but all of which have two variables
named "A1," "A2," and "A3". I want to create a new variable in each of the
three data frames that contains the value held in "A1" if A3 contains a
value greater than zero, and the value held in "A2" if A3 contains a value
less than zero. Seems simple enough, right?
My attempt at this code uses this faux-data:
set.seed(1)
A1=seq(1,100,length=100)
A2=seq(-100,-1,length=100)
A3=runif(100,-1,1)
df1=cbind(A1,A2,A3)
A3=runif(100,-1,1)
df2=cbind(A1,A2,A3)
I'm about a thousand percent sure that R has some functionality for
creating the same named variable in multiple data frames, but I have tried
doing this with lapply:
mylist=list(df1,df2)
lapply(mylist,function(x){
x$newVar=x$A1
x$newVar[x$A3>0]=x$A2
})
With for loops:
for(df in mylist){
df$newVar=df$A1
df$newVar[df$A3>0]=df$A2
}
But I always get some variation of the error:
Error in x$A1 : $ operator is invalid for atomic vectors
Any help would be appreciated.
Thank you.

No comments:

Post a Comment