abel
HI,
I have 16 data frames (each with 2 columns) named a02, a03,..., a09,
b02, b03,..., b09. I would like to apply to the 2d column of each data
frame the same function and get back the result in the 3d column of
each data frame. The fuction is something like:
ifelse(value of column 2 < 1, 1, ifelse(value of column 2 < 5, 2 ,3))
I played with eval, parse, paste... in a loop, but I can't get the
solution. I have only 3 lines of code in order to call the data
frames:
x_c("a","b")
y_c("02","03","04","05","06","07","08","09")
u_outer(x,y,paste,sep="")
Thank you for your help
abel
类似于SAS、SPSS中使用macro来处理的。
实际工作中也许会经常遇到,我给出了一个回答,算是抛砖引玉,希望有更好、更简洁的解决方法。
dfs.name <- as.character(u) # u <- outer(x, y, paste, sep="")
myfun <- function(x)
{
d.f <- get(x)
d.f$col3 <- ifelse(d.f$col2 < 1, 1, ifelse(d.f$col2 < 5, 2 ,3))
assign(x, d.f, pos=1)
}
sapply(dfs.name, myfun)