在dplyr中连接两个文本列

mmy*_*g77 6 r string-concatenation dplyr

我的数据如下所示:

round <- c(rep("A", 3), rep("B", 3))
experiment <- rep(c("V1", "V2", "V3"), 2)
results <- rnorm(mean = 10, n = 6)

df <- data.frame(round, experiment, results)

> df
  round experiment   results
1     A         V1  9.782025
2     A         V2  8.973996
3     A         V3  9.271109
4     B         V1  9.374961
5     B         V2  8.313307
6     B         V3 10.837787
Run Code Online (Sandbox Code Playgroud)

我有一个不同的数据集将与此合并,其中每个组合roundexperiment是一个唯一的行值,即"A_V1".所以我真正想要的是一个name将两列连接在一起的变量.然而,在dplyr中这比我预期的要困难得多.我试过了:

name_mix <- paste0(df$round, "_", df$experiment)
new_df <- df %>%
  mutate(name = name_mix) %>%
  select(name, results)
Run Code Online (Sandbox Code Playgroud)

但是我得到了错误Column name must be length 1 (the group size), not 6.我也尝试了简单的base-R方法,cbind(df, name_mix)但收到了类似的错误,告诉我df并且name_mix大小不同.我究竟做错了什么?

DJV*_*DJV 8

您可以使用该unite功能tidyr

require(tidyverse)

df %>% 
  unite(round_experiment, c("round", "experiment"))

  round_experiment   results
1             A_V1  8.797624
2             A_V2  9.721078
3             A_V3 10.519000
4             B_V1  9.714066
5             B_V2  9.952211
6             B_V3  9.642900
Run Code Online (Sandbox Code Playgroud)


San*_*ndy 6

你也可以尝试这个:

library(tidyr)
library(dplyr)
df = df %>% 
   unite(combined, round, experiment, sep = "_", remove = FALSE)
Run Code Online (Sandbox Code Playgroud)

输出将是:

combined round experiment   results
 A_V1     A         V1      10.152329
 A_V2     A         V2      10.863128
 A_V3     A         V3      10.975773
 B_V1     B         V1       9.964696
 B_V2     B         V2       9.876675
 B_V3     B         V3       9.252936
Run Code Online (Sandbox Code Playgroud)

这将保留您的原始列。


小智 5

如果您正在寻找新变量,这应该可以解决问题

library(tidyverse)

round <- c(rep("A", 3), rep("B", 3))
experiment <- rep(c("V1", "V2", "V3"), 2)
results <- rnorm(mean = 10, n = 6)

df <- data.frame(round, experiment, results)
df

df <- df %>% mutate(
  name = paste(round, experiment, sep = "_")
)
Run Code Online (Sandbox Code Playgroud)