Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all rows where length of string is more than n

Tags:

dataframe

r

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]
like image 898
megv Avatar asked Dec 27 '11 01:12

megv


4 Answers

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

m %>% dplyr::filter(nchar(f_name) > 3)
like image 150
elcortegano Avatar answered Dec 06 '22 13:12

elcortegano


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)
like image 45
neilfws Avatar answered Dec 06 '22 13:12

neilfws


Try this:

m[!nchar(as.character(m$f_name)) > 3, ]
like image 29
oeo4b Avatar answered Dec 06 '22 13:12

oeo4b


The obligatory data.table solution:

setDT(m)
m[ nchar(f_name) <= 3 ]
like image 40
andschar Avatar answered Dec 06 '22 13:12

andschar