使用 R 完成该列,只要另一列中有文本即可

Complete the column, as far as there is text in the other column with R

提问人:Francisco1988 提问时间:11/12/2023 最后编辑:Francisco1988 更新时间:11/12/2023 访问量:44

问:

我需要完成该列,只要另一列中有文本

当前数据帧

first_column <- c("eeer","","","sdfdfdsf", "sdfsdfdsf","faffa", "we", "", "", "", "", "", "eeee", "", "wqwq", "", "ttetxg", "", "sf", "sf","f", "dfsdf", "sqdweg", "sfsfdsdvghhh", "")
second_column <- c("","", "","", "", "", "", "renato paulo", "", "", "", "", "", "carlos gomes", "", "", "", "vitor", "","","","","","", "Cassia rabelo")
current_dataframe <- data.frame(first_column, second_column)

所需输出

first_column <- c("eeer","","","sdfdfdsf", "sdfsdfdsf","faffa", "we", "", "", "", "", "", "eeee", "", "wqwq", "", "ttetxg", "", "sf", "sf","f", "dfsdf", "sqdweg", "sfsfdsdvghhh", "")
second_column <- c("","", "","renato paulo", "renato paulo", "renato paulo", "renato paulo", "", "", "", "", "", "carlos gomes", "", "", "", "vitor", "", "Cassia rabelo","Cassia rabelo","Cassia rabelo","Cassia rabelo","Cassia rabelo","Cassia rabelo", "")
desired_output <- data.frame(first_column, second_column)

我尝试使用填充向下调功能,但它没有按我的需要工作。谁能帮我?

r dplyr tidyverse tidy mutate

评论

0赞 Andre Wildberg 11/12/2023
第二列在“当前”数据帧中向下移动一行。这是对的吗?

答:

0赞 Andre Wildberg 11/12/2023 #1

一种方法包括将second_column上移 1。

library(dplyr) # dplyr > 1.1.0 for *consecutive_id()*

data.frame(first_column = current_dataframe$first_column, 
           second_column = c(
             current_dataframe$second_column[2:nrow(current_dataframe)], "")) %>% 
  group_by(grp = consecutive_id(first_column != "")) %>% 
  mutate(second_column = last(second_column)) %>% 
  ungroup() %>% 
  select(-grp) %>% 
  data.frame()
   first_column second_column
1          eeer              
2                            
3                            
4      sdfdfdsf  renato paulo
5     sdfsdfdsf  renato paulo
6         faffa  renato paulo
7            we  renato paulo
8                            
9                            
10                           
11                           
12                           
13         eeee  carlos gomes
14                           
15         wqwq              
16                           
17       ttetxg         vitor
18                           
19           sf Cassia rabelo
20           sf Cassia rabelo
21            f Cassia rabelo
22        dfsdf Cassia rabelo
23       sqdweg Cassia rabelo
24 sfsfdsdvghhh Cassia rabelo
25                           

评论

0赞 Francisco1988 11/12/2023
中的错误: !在 中添加计算列时出现问题。x 列 有问题。我。x 找不到功能“consecutive_id”由以下错误引起: !列 有问题。我。x 找不到功能“consecutive_id”由以下错误引起: !找不到功能“consecutive_id”add_computed_columns()group_by()mutate()grpgrp = consecutive_id(first_column != "")mutate_cols()mutate()grpgrp = consecutive_id(first_column != "")consecutive_id()
0赞 Andre Wildberg 11/12/2023
dplyr.tidyverse.org/reference/consecutive_id.html
0赞 Francisco1988 11/12/2023
这些列的行数相同
0赞 Andre Wildberg 11/12/2023
consecutive_id在 dplyr > 1.1.0 中可用。如果无法更新、添加和使用library(data.table)... group_by(grp = rleid(first_column != "")) ...