简体   繁体   中英

Removing rows in R dataframe based on the number of words in one column

I'm trying to subset a data.frame in R based on the number of words in one column. There will only be one or two words (so the presence of a white space in the string could be used instead) - I want to keep rows with two words, and delete those with one. For example, for df below, I want to keep rows 1,2,4 and 6.

df <- data.frame(id = c("one one","one two","one","two one","two","two one"),
               data = seq(1:6))

Any help would be much appreciated.

You could filter the id's with a white space

library(tidyverse)

df %>% 
  filter(str_detect(id," "))

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM