I have a list of data frames (here is an example 2).
df1 <- read.table(text= "var,X2x,X4x,X6x
101337,4.631833,4.4547,11.097333
345754,3.727433,10.8560,10.536600" ,header=TRUE, sep=",")
df2 <- read.table(text= "var,X2x,X4x,X6x
101337,5.631833,10.4547,11.097333
345754,5.727433,12.8560,10.536600" ,header=TRUE, sep=",")
dflist <- list(df1=df1, df2=df2)
I wanted to use lapply to go through each data.frame file, and use it for a simple comparison (i.e. check if the value from the second column is greater than the third), taking into account the result, add a new column with a tag (in the example, the new column is called " factor. ")
I'm almost there, but the output of my script is incorrect, returning a list of vectors instead of a list of data.frames with an added column.
here is the code:
dfL <- lapply(dflist,function(dfx) {
apply(dfx,1, function(df) { if(df[3] < (df[4] )) {
dfx$factor<-"nonNA"} else {dfx$factor<-"NA"}
}
)
}
)
Could you explain to me what I'm doing wrong?