Home > Software engineering >  Searching for a way to replace elements of character vectors in a list
Searching for a way to replace elements of character vectors in a list

Time:01-18

I have a list of character vectors of different length, containing identifiers (e.g. "011" or "12"), numbers indicating the amount of money ("112.3" or "490.5") and years ("2011" or "2020"), empty elements ("") and elements only containing a dot("."). I want to get rid of the elements of character vectors that only contain a dot or are empty. The leading zeros of the identifiers are important, so I cannot change the type to numeric.

This original data

list <- list(c("2015","2016"),c(""),c("."), c("0","2418.9","292.4"),c("2",".",".","2394.6"))

should look like this:

list_final <- list(c("2015","2016"),c("0","2418.9","292.4"),c("2","2394.6"))

My idea is to create a list with TRUE/FALSE indicating for each vector which elements to keep, but right now I'm really stuck as the following approach does not work (it returns integers that are zero) :

test <- lapply(list, function(i) {unlist(lapply(list[i], function(b) which(b==".")))})

Regarding the expression for ".", I already tried other regular expressions like "\." and "[.]".

CodePudding user response:

We could loop over the list, subset the elements that are not . or "" and Filter out the list elements that are empty

Filter(length, lapply(list, function(x) x[! x %in% c(".", "")]))

-output

[[1]]
[1] "2015" "2016"

[[2]]
[1] "0"      "2418.9" "292.4" 

[[3]]
[1] "2"      "2394.6"

CodePudding user response:

A two-step solution doubling down on lapply, if needed/as an alternative:

# data
l <- list(c("2015","2016"),c(""),c("."), c("0","2418.9","292.4"),c("2",".",".","2394.6"))

# remove those with "." or ""
l2 <- lapply(l, function(x) {x[!(x %in% c(".", ""))]})

# remove empty list positions
l2[lapply(l2, length) > 0]

#[[1]]
#[1] "2015" "2016"

#[[2]]
#[1] "0"      "2418.9" "292.4" 

#[[3]]
#[1] "2"      "2394.6"
  • Related