r - 如何逐个元素地组合数据框中的两列?
问题描述
我需要逐个元素地组合数据框中的两列。我尝试使用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
推荐阅读
- python - 如何获取“描述”中匹配的字符串值
- typescript - 打字稿 - 将 array.map 转换为数组
- autodesk-forge - Forge vuer 不渲染 revit 模型
- python - Geopy reverse 返回“city”、“district”和“borough”的键错误,尽管它位于 JSON 响应对象中
- sql - 使用 T-SQL 将 JSON 格式的数据导出到 SQL 的最佳方法是什么?
- docker - 在 Windows 10 + WSL2 上运行 nvidia-docker
- excel - 如何在excel VBA中使用匹配大小写
- python - 尝试在 python 中加载 bmp 图像时,数组列的顺序未对齐
- python - 在 heroku 上托管我的应用程序时(特别是在尝试迁移到 heroku 时)我收到一条错误消息,提示找不到模块
- python - 为注销 ami 编写 lambda 函数,并删除相应的快照