连接dplyr中的两个文本列 [英] Concatenating two text columns in dplyr

查看:49
本文介绍了连接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

我有一个不同的数据集,将与该数据集合并,其中 round experiment 的每个组合都是唯一的行值,即"A_V1".因此,我真正想要的是将两个列连接在一起的变量 name .但是,在dplyr中这样做比我预期的要难.我试过了:

I have a different dataset that will be merged with this one where each combo of round and experiment is a unique row value, ie, "A_V1". So what I really want is a variable name that concatenates the two columns together. However, this is tougher to do in dplyr than I expected. I tried:

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

但是我得到了错误,列名必须是长度1(组大小),而不是6 .我还尝试了 cbind(df,name_mix)的简单base-R方法,但收到一个类似的错误,告诉我 df name_mix 不同的大小.我在做什么错了?

But I got the error, Column name must be length 1 (the group size), not 6. I also tried the simple base-R approach of cbind(df, name_mix) but received a similar error telling me that df and name_mix were of different sizes. What am I doing wrong?

推荐答案

您可以使用 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

这篇关于连接dplyr中的两个文本列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆