lst1是一个列表:
lst1 <- list(c("all the apples","apples in the tables","fashion prosthetics"),c("meteorological concepts","effects of climate change","environmental"))
> lst1 [[1]] [1] "all the apple" "apple in the table" "nature" [[2]] [1] "meteorological concept" "effect of climate change" [3] "environmental"
我试过了
gsub("\\'s|s$|s[[:space:]]{0}","",lst1)
但它不保留列表结构.
怎么做到呢?
解决方法
您可以使用带有lapply的gsub循环遍历列表元素
lapply(lst1,gsub,pattern= "\\'s|s$|s\\b",replacement='') #[[1]] #[1] "all the apple" "apple in the table" "fashion prosthetic" #[[2]] #[1] "meteorological concept" "effect of climate change" #[3] "environmental"