2021年5月2日星期日

R: Identify non-NA values from one column and copy the values from another column based on the remaining rows

I have a data frame (df) with multiple columns (45) and rows (20,000):

I want to filter each variable column by selecting only the rows with non-NA values and creating a separate data frame with the corresponding ID and Name for the rows selected. I then want to save each data frame with the corresponding variable name. For example, the output data frames would look like this and would be saved as Var1 and Var2 respectively.

Var 1 <

enter image description here

Var 2 <

enter image description here

I am currently trying to use this function on R and thinking of implementing a for loop.

df2 = lapply(df, function(x) {x[!is.na(x)]}).

This hasn't worked so well as it does not list the values from corresponding ID and Name column. This also doesn't create a dataframe.

Any suggestions will be greatly appreciated!

https://stackoverflow.com/questions/67363068/r-identify-non-na-values-from-one-column-and-copy-the-values-from-another-colum May 03, 2021 at 11:07AM

没有评论:

发表评论