I have a list of character strings where there are repeats in some of the strings. For example:
[[1]]
[1] "gr gal gr gal"
[[2]]
[1] "gr gal"
[[3]]
[1] "gr gal ir ol"
[[4]]
[1] "gr gal gr gal"
[[5]]
[1] "gr gal"
My desired output is:
[[1]]
[1] "gr gal"
[[2]]
[1] "gr gal"
[[3]]
[1] "gr gal ir ol"
[[4]]
[1] "gr gal"
[[5]]
[1] "gr gal"
Where the repeats are removed from the string.
My plan is to call strsplit(x, split = " ") and then call the unique function on the split object. If I do it choosing 1 member of the list, my code works fine:
> strsplit(pathmd1[[76]], split = " ")
[[1]]
[1] "gr" "gal" "gr" "gal"
> splittest <- strsplit(pathmd1[[76]], split = " ")
> unique(unlist(splittest))
[1] "gr" "gal"
However, when I use lapply using these functions, an error is thrown
pathmd2 <- lapply(1:length(pathmd1), function(i) strsplit(pathmd1[[i]],
split = " "))
pathmd <- lapply(1:length(pathmd2), function(i) unique(pathmd2[[i]])
unexpected symbol
77: pathmd2 <- lapply(1:length(pathmd1), function(i)
strsplit(pathmd1[[i]], split = " ")
78: pathmd
^
Why isn't the function working with lapply?
You can try:
lapply(f, function(x) unique(unlist(strsplit(x, " "))))
#output
[[1]]
[1] "gr" "gal"
[[2]]
[1] "gr" "gal"
[[3]]
[1] "gr" "gal" "ir" "ol"
[[4]]
[1] "gr" "gal"
[[5]]
[1] "gr" "gal"
where f is your list.
there is not need to iterate like with a for
loop
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.