I have a list where I would like to remove empty characters: ""
.
I seem to be subsetting the elements incorrectly:
> sample2[which(sample2 == "")]
list()
> sample2[which(sample2 != "")]
[[1]]
[1] "" "03JAN1990" "" "" ""
[6] "" "23.4" "0.4" "" ""
[11] "" "" "25.1" "0.3" ""
[16] "" "" "" "26.6" "0.0"
[21] "" "" "" "" "28.6"
[26] "0.3"
What should I do to subset and remove the empty characters?
From your output, it looks like sample2
is not a character vector, but it is a list containing a character vector. You should be using
sample2[[1]][which(sample2[[1]] != "")]
(It would help to include dput(sample2)
just to confirm)
Or even better, take the character vector out of the list first
sample3 <- sample2[[1]]
# or maybe sample3 <- unlist(sample2)
sample3[which(sample3 != "")]
A very basic solution:
> lst = list(1,2,"dog","","boss","")
> x = unlist(lst)
> list(x[x!=""])
[[1]]
[1] "1" "2" "dog" "boss"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.