簡體   English   中英

如何將一個數據幀中的兩列逐元素組合?

[英]How to combine two columns in a data frame element by element?

我需要在數據框中逐元素組合兩列。 我嘗試使用paste功能,但基本上是將列連接在一起,這不是我所需要的:

#sample data
df <- data.frame ("col1" = c("red|",
                             "blue| , red|", 
                             "blue| , red| , yellow|"), 
                  "col2" = c("green",
                             "yellow , blue",
                             "black , red , blue"))

#this is what I tried:
df$new <- paste(df$col1, df$col2, sep = " , ")

#output for each row:
# "red| , green"           
# "blue| , red| , yellow , blue"            
# "blue| , red| , yellow| , black , red , blue"

#below is the desired output:
df$correct_output <- c("red|green",
                       "blue|yellow , red|blue",
                       "blue|black , red|red , yellow|blue")
#sample data
df <- data.frame ("col1" = c("red|",
                             "blue| , red|", 
                             "blue| , red| , yellow|"), 
                  "col2" = c("green",
                             "yellow , blue",
                             "black , red , blue"))

library(tidyverse)

df %>%
  group_by(id = row_number()) %>%           # group by a row id (useful to reshape)
  separate_rows(col1, col2, sep=" ,") %>%   # separate based on comma and add new rows
  unite(col, col1, col2, sep="") %>%        # combine corresponding values
  summarise(correct = paste0(gsub(" ", "", col), collapse = ", ")) %>% # remove any spaces and combine values
  bind_cols(df, .) %>%                      # bind origina dataset
  select(-id)                               # remove id column

#                     col1               col2                          correct
# 1                   red|              green                        red|green
# 2           blue| , red|      yellow , blue            blue|yellow, red|blue
# 3 blue| , red| , yellow| black , red , blue blue|black, red|red, yellow|blue
df <- data.frame ("col1" = c("red|",
                             "blue| , red|", 
                             "blue| , red| , yellow|"), 
                  "col2" = c("green",
                             "yellow , blue",
                             "black , red , blue"),
                  stringsAsFactors = F)
parts1 <- strsplit(df$col1, ' , ')
parts2 <- strsplit(df$col2, ' , ')
# join parts from two columns
n <- dim(df)[1]
df$col3 <- lapply(1:n, function(i) paste0(parts1[[i]], parts2[[i]])) 
# join joined parts to a single string per row
df$col3 <- lapply(col3, function(x) paste(x, collapse = ' , '))
df

                    col1               col2                               col3
1                   red|              green                          red|green
2           blue| , red|      yellow , blue             blue|yellow , red|blue
3 blue| , red| , yellow| black , red , blue blue|black , red|red , yellow|blue

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM