Arjun Mohan
Arjun Mohan

Reputation: 63

Conditionally replacing specific values from a list of lists in R

Tried to look for a similar question because I think someone may have asked this before to do but I've not been able to so here goes.

I've got a list of lists in my dataset, which contain characters. For example:

data$list_items[1]
[[1]]
[1] "633" "0" 

data$list_items[2]
[[1]]
[1] "0"

data$list_items[3]
[[1]]
[1] "0" "357" "-1" "A21"

And so on. They're numbers but they're stored as chars because some of the item codes have letters in them. All the rows contain at least "0" which is like a filler for NAs in the columns which I merged to make the list of lists, while some of the lists also contain "-1" which just means undefined.

I basically just want to get rid of the "0" and "-1" elements, and replace those remaining rows which may end up becoming empty sets with the letters "NA".

I did this using a for loop by replacing them all with "NA" then removing duplicates and removing "NA" from all lists of length>1, but is there a way to use one of the apply functions like this? I'm getting stuck on the step where the empty lists get replaced with "NA"

In the 3 cases I showed for example, my end result would be:

data$list_items[1]
[[1]]
[1] "633" 

data$list_items[2]
[[1]]
[1] "NA"

data$list_items[3]
[[1]]
[1] "357" "A21"

Upvotes: 0

Views: 239

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389265

You can try using lapply -

data$list_items <- lapply(data$list_items, function(x) {
  #Drop 0 and -1 value
  x <- x[!x %in% c(0, -1)]
  #If empty return NA
  if(length(x)) x else NA
})

Upvotes: 2

Related Questions