Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 204742

Add column to elements within list of dataframes

$
0
0

I have a list of data frames where some have a need column and others do not. How do I add the need column to the other data frames (with value = NA)? I've tried using Map or lapply

test <- list(data.frame(need = NA, dont_need = NA),
             data.frame(dont_need = NA),
             data.frame(dont_need = NA, dont_need_2 = NA))

Desired Output

[[1]]
  need dont_need
1   NA        NA

[[2]]
  dont_need need
1        NA   NA

[[3]]
  dont_need dont_need_2 need
1        NA          NA   NA

I can't just use a bind because this list is dynamically created and sometimes only includes one dataframe that doesn't have the need column. The answer must also work when this is the case:

test_2 <- list(data.frame(dont_need = NA))

Viewing all articles
Browse latest Browse all 204742

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>