基于一列中的文本更新 Dataframe

nukf8bse  于 2023-04-27  发布在  其他
关注(0)|答案(1)|浏览(91)

我想转换的数据框的基础上,列的房子。因此,如果列的房子有文字分隔的“;“将文本添加到新行中。并添加新列head 1,head 2

library(dplyr)
library(tidyr)

df <-  data.frame(Region = c("AU","USA","CA","UK","GE","AU","USA","CA","UK"),
                  lock = c(1,1,NA,1,NA,1,NA,1,NA),
                  type= c("sale",NA,NA,"target","target",NA,"sale",NA,"target"),
                  House =c("Tagore house","Gandhi house",NA,"Flexible;Tagore house;Gandhi house","Tagore house",NA,"Flexible;Gandhi house","Gandhi house","Gandhi house"))

df %>%
  mutate(House = strsplit(House, ";"),
         head= head) %>%
  unnest(House)

输出应该如下所示

xzlaal3s

xzlaal3s1#

试试看

library(dplyr)
library(stringr)
library(tidyr)
df %>%
   mutate(rn = row_number()) %>% 
   separate_rows(House, sep = ";") %>% 
   group_by(rn) %>% 
   mutate(Head = if(n() > 1) c(str_c("head", seq_len(n()-1)), "") else "", 
 across(c(Region, lock, type), ~ replace(.x, row_number() != n(), ""))) %>%
  ungroup %>%
   select(-rn)
  • 输出
# A tibble: 12 × 5
   Region lock  type     House        Head   
   <chr>  <chr> <chr>    <chr>        <chr>  
 1 "AU"   "1"   "sale"   Tagore house ""     
 2 "USA"  "1"    <NA>    Gandhi house ""     
 3 "CA"   <NA>   <NA>    <NA>         ""     
 4 ""     ""    ""       Flexible     "head1"
 5 ""     ""    ""       Tagore house "head2"
 6 "UK"   "1"   "target" Gandhi house ""     
 7 "GE"   <NA>  "target" Tagore house ""     
 8 "AU"   "1"    <NA>    <NA>         ""     
 9 ""     ""    ""       Flexible     "head1"
10 "USA"  <NA>  "sale"   Gandhi house ""     
11 "CA"   "1"    <NA>    Gandhi house ""     
12 "UK"   <NA>  "target" Gandhi house ""

相关问题