assign NA within listcolumn using map and purrr

363 views Asked by At
library(tidyverse)    
k<-data_frame(u=c("A","B"),l=c(list(c(1,5,4)),list(c(7,5,1))))

I want to set all values below 2 in the listcolumn l to NA. How can I do this within the purrr-world using map?

map(k$l,~.x[.x<2]<-NA)

throws an error.

update:

k %>% mutate(o=map(l, ~(.x[.x<2]<-NA)))

gives me the extra column, but not with two numbers and one NA in each element of the listcolumn

Update II: Replace is my friend and does the trick:

k %>% mutate(o=map(l, ~replace(.x,.x<2,NA)))
1

There are 1 answers

0
Misha On BEST ANSWER
k %>% mutate(o=map(l, ~replace(.x,.x<2,NA)))

As mentioned in the final edit - the above command solves my problems. Posted after the suggestion by comment above.