Remove all rows where length of string is more than n

人走茶凉 提交于 2020-01-20 19:29:55

问题


I have a dataframe m and I want to remove all the rows where the f_name column has an entry greater than 3. I assume I can use something similar to

m <- m[-grep("nchar(m$f_name)>3", m$f_name]

回答1:


To reword your question slightly, you want to retain rows where entries in f_name have length of 3 or less. So how about:

subset(m, nchar(as.character(f_name)) <= 3)



回答2:


Try this:

m[!nchar(as.character(m$f_name)) > 3, ]



回答3:


For those looking for a tidyverse approach, you can use dplyr::filter:

m %>% dplyr::filter(nchar(f_name) > 3)


来源:https://stackoverflow.com/questions/8640377/remove-all-rows-where-length-of-string-is-more-than-n

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!