Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lapply over list of dataframes to subset and overwrite dfs

Tags:

r

I am trying to subset each dataframe to exclude rows in which the first column is NA or "". I tried putting the dataframes into a list df and then using lapply over each dataframe. The code works, only that I am not sure how to overwrite each dataframe with the subset.

df1 <- data.frame(v1=c(1, 2, 3, NA, NA, NA), v2=rep(1, 6))
df2 <- data.frame(v11=c(2, 3, 4, 5, NA, ""), v22=rep(1, 6))
df3 <- data.frame(v111=c(3, 4, 5, 6, 7, NA), v222=rep(1, 6))

df <- list(df1=df1, df2=df2, df3=df3)
df

$df1
# v1 v2
# 1  1  1
# 2  2  1
# 3  3  1
# 4 NA  1
# 5 NA  1
# 6 NA  1
# 
# $df2
# v11 v22
# 1    2   1
# 2    3   1
# 3    4   1
# 4    5   1
# 5 <NA>   1
# 6        1
# 
# $df3
# v111 v222
# 1    3    1
# 2    4    1
# 3    5    1
# 4    6    1
# 5    7    1
# 6   NA    1

lapply(names(df), function(x) df[[x]][!(is.na(df[[x]][,1]) | df[[x]][,1]==""), ])

# [[1]]
# v1 v2
# 1  1  1
# 2  2  1
# 3  3  1
# 
# [[2]]
# v11 v22
# 1   2   1
# 2   3   1
# 3   4   1
# 4   5   1
# 
# [[3]]
# v111 v222
# 1    3    1
# 2    4    1
# 3    5    1
# 4    6    1
# 5    7    1

In the end, I want df3, for example, to be as follows:

df3
#  v111 v222
#1    3    1
#2    4    1
#3    5    1
#4    6    1
#5    7    1
like image 691
Eric Green Avatar asked Aug 28 '14 19:08

Eric Green


2 Answers

You can simplify your lapply to the following form (in order to keep the names of the data frames too)

df <- lapply(df, function(x) x[!(is.na(x[1]) | x[1] == ""), ])

Then use list2env in order to get you data frames back into the global environment

list2env(df, .GlobalEnv)

Then you can inspect your new data frames by just

df1
##   v1 v2
## 1  1  1
## 2  2  1
## 3  3  1

etc.

like image 144
David Arenburg Avatar answered Nov 10 '22 17:11

David Arenburg


Is this what you are looking for?

df <- lapply(
  names(df),
  function(x){
    df[[x]][!(is.na(df[[x]][,1]) | df[[x]][,1]==""), ]
  })

which gives you

> df
[[1]]
  v1 v2
1  1  1
2  2  1
3  3  1

[[2]]
  v11 v22
1   2   1
2   3   1
3   4   1
4   5   1

[[3]]
  v111 v222
1    3    1
2    4    1
3    5    1
4    6    1
5    7    1
like image 1
nrussell Avatar answered Nov 10 '22 17:11

nrussell