完成该列,只要另一列中有R的文本

c9qzyr3d  于 2023-11-14  发布在  其他
关注(0)|答案(1)|浏览(134)

我需要完成列,只要有文字在其他列与
当前帧

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)


我试着使用fill downup功能,它不像我需要的那样工作。有人能帮助我吗?

6ojccjat

6ojccjat1#

一种方法,包括将 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

字符串

相关问题