仅将 tidyr 单独应用于特定行 [英] Applying tidyr separate only to specific rows

查看:39
本文介绍了仅将 tidyr 单独应用于特定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 tidyr 来分隔数据框中的一列,同时仅将其应用于特定行.虽然 dplyr::filter 完成了这项工作,但它忽略了我的其余数据.是否有一种干净的方法可以将 tidyr 应用于特定行,同时保持其余数据不变?

这是我的问题的一个例子:

#为示例创建 DFdf<-data.frame(var_a=letters[1:5],var_b=c(sample(1:100,5)),text=c("foo_bla","here_do","oh_yes","baa","land"))

给我这个:

<块引用>

 var_a var_b 文本1 10 foo_bla2 b 58 here_do3 c 34 oh_yes4 天 1 巴5 e 47 土地

#separating 一列:clean_df<-df %>% 分离(文本,进入=c(第一",秒"),sep=_",删除=F)clean_df

<块引用>

 var_a var_b 文本第一秒1 a 10 foo_bla foo bla2 b 58 here_do here do3 c 34 oh_yes 哦是的4 d 1 baa baa <NA>5 e 47 陆地土地<NA>

我只想拆分here_do"行.在此先感谢您的任何帮助!

解决方案

另一种方法:

cols_to_split = c('here_do')clean_df <-df %>%过滤器(文本 %in% cols_to_split)%>%tidyr::separate(text,into=c("first","sec"),sep="_",remove=F)%>%bind_rows(过滤器(df,!text %in% cols_to_split))# var_a var_b 文本第一秒#1 b 7 here_do here do#2 a 26 foo_bla <不适用>#3 c 23 oh_yes <NA><不适用>#4 d 2 baa <NA><不适用>#5 e 67 陆地 <NA><不适用>

如果您需要将其余行保留在第一"列中,您可以使用:

clean_df <-df %>%过滤器(文本 %in% cols_to_split)%>%tidyr::separate(text,into=c("first","sec"),sep="_",remove=F)%>%bind_rows(filter(df, !text %in% cols_to_split)) %>%mutate(first = ifelse(is.na(first), as.character(text), first))# var_a var_b 文本第一秒#1 b 7 here_do here do#2 a 26 foo_bla foo_bla #3 c 23 oh_yes oh_yes <NA>#4 d 2 baa baa <NA>#5 e 67 陆地 <NA>

I'm trying to use tidyr to separate one column in my data frame, while applying it only to specific rows. While dplyr::filter does the job, it omits the rest of my data. Is there a clean way to apply tidyr to specific rows while keeping the rest of the data untouched?

here is an example of my problem:

#creating DF for the example
df<-data.frame(var_a=letters[1:5],
               var_b=c(sample(1:100,5)),
               text=c("foo_bla","here_do","oh_yes","baa","land"))

gives me this:

  var_a var_b    text
1     a    10 foo_bla
2     b    58 here_do
3     c    34  oh_yes
4     d     1     baa
5     e    47    land

#separating one col:
clean_df<-df %>% separate(text,into=c("first","sec"),sep="_",remove=F)
clean_df

  var_a var_b    text first  sec
1     a    10 foo_bla   foo  bla
2     b    58 here_do  here   do
3     c    34  oh_yes    oh  yes
4     d     1     baa   baa <NA>
5     e    47    land  land <NA>

I want to split only the "here_do" row. Thanks in advance for any kind of help!

解决方案

Another approach:

cols_to_split = c('here_do')

clean_df <-df %>% 
     filter(text %in% cols_to_split) %>% 
     tidyr::separate(text,into=c("first","sec"),sep="_",remove=F) %>% 
     bind_rows(filter(df, !text %in% cols_to_split))


#  var_a var_b    text first  sec
#1     b     7 here_do  here   do
#2     a    26 foo_bla  <NA> <NA>
#3     c    23  oh_yes  <NA> <NA>
#4     d     2     baa  <NA> <NA>
#5     e    67    land  <NA> <NA>

If you need to keep rest of the rows in column 'first', you may use:

clean_df <-df %>% 
     filter(text %in% cols_to_split) %>% 
     tidyr::separate(text,into=c("first","sec"),sep="_",remove=F) %>% 
     bind_rows(filter(df, !text %in% cols_to_split)) %>% 
     mutate(first = ifelse(is.na(first), as.character(text), first))

#  var_a var_b    text   first  sec
#1     b     7 here_do    here   do
#2     a    26 foo_bla foo_bla <NA>
#3     c    23  oh_yes  oh_yes <NA>
#4     d     2     baa     baa <NA>
#5     e    67    land    land <NA>

这篇关于仅将 tidyr 单独应用于特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆