Remove all rows where length of string is more than n

后端 未结 4 1865
独厮守ぢ
独厮守ぢ 2020-12-05 16:21

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

相关标签:
4条回答
  • 2020-12-05 16:47

    Try this:

    m[!nchar(as.character(m$f_name)) > 3, ]
    
    0 讨论(0)
  • 2020-12-05 16:58

    The obligatory data.table solution:

    setDT(m)
    m[ nchar(f_name) <= 3 ]
    
    0 讨论(0)
  • 2020-12-05 17:01

    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)
    
    0 讨论(0)
  • 2020-12-05 17:08

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

    m %>% dplyr::filter(nchar(f_name) > 3)
    
    0 讨论(0)
提交回复
热议问题