如何在两个数据框之间匹配列名并添加新变量?

huangapple go评论72阅读模式
英文:

How to match column names across two dataframes and add a new variable?

问题

output <- rbind(c("Cat1", "Cat2", "Cat3", "Cat2", "Cat5", "NA"),
                c(1, 2, 3, 4, 5, 6),
                c(1, 2, 3, 4, 5, 6),
                c(1, 2, 3, 4, 5, 6))
colnames(output) <- colnames(data)
英文:
data <- rbind(c(1,2,3,4,5,6),
              c(1,2,3,4,5,6),
              c(1,2,3,4,5,6))

colnames(data) <- c("A", "B", "C", "D", "E", "F")

category_vector <- cbind(c("A", "B", "C", "D", "E"),
                         c("Cat1", "Cat2", "Cat3", "Cat2", "Cat5"))
colnames(category_vector) <- c("Name", "Category")

My goal is to match column names in data, to the names in category_vector and then assign each column of data the corresponding category name in Row 1.

My desired output is as follows:

output <- rbind(c("Cat1", "Cat2", "Cat3", "Cat2", "Cat5", "NA"),
                c(1,2,3,4,5,6),
                c(1,2,3,4,5,6),
                c(1,2,3,4,5,6))
colnames(output) <- colnames(data)

I know, that I can use match(names(x), names(y)) to show the intersect, but I am not sure how to get to the desired output.

答案1

得分: 0

这是一个例子:

library(dplyr)

data <- rbind(c(1,2,3,4,5,6),
             c(1,2,3,4,5,6),
             c(1,2,3,4,5,6))

colnames(data) <- c("A", "B", "C", "D", "E", "F")

category_vector <- cbind(c("A", "B", "C", "D", "E"),
                         c("Cat1", "Cat2", "Cat3", "Cat2", "Cat5"))

#colnames(category_vector) <- c("Name", "Category")

# 将data中的所有内容转换为字符型
data <- data %>%
    as.data.frame() %>%
    mutate(across(everything(), as.character))

category_vector <- as.data.frame(t(category_vector))

names(category_vector) <- category_vector[1, ]

category_vector <- category_vector[-1, ]

category_vector %>%
    bind_rows(data)

希望这对你有所帮助。

英文:

Here is one example:

library(dplyr)

data &lt;- rbind(c(1,2,3,4,5,6),
             c(1,2,3,4,5,6),
             c(1,2,3,4,5,6))

colnames(data) &lt;- c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;, &quot;F&quot;)

category_vector &lt;- cbind(c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;),
                         c(&quot;Cat1&quot;, &quot;Cat2&quot;, &quot;Cat3&quot;, &quot;Cat2&quot;, &quot;Cat5&quot;))

#colnames(category_vector) &lt;- c(&quot;Name&quot;, &quot;Category&quot;)

# turn everything in data into a character
data &lt;- data |&gt; 
    as.data.frame() |&gt; 
    mutate(across(everything(), as.character))

category_vector &lt;- as.data.frame(t(category_vector))

names(category_vector) &lt;- category_vector[1, ]

category_vector &lt;- category_vector[-1, ]

category_vector |&gt; 
    bind_rows(data)

     A    B    C    D    E    F
1 Cat1 Cat2 Cat3 Cat2 Cat5 &lt;NA&gt;
2    1    2    3    4    5    6
3    1    2    3    4    5    6
4    1    2    3    4    5    6

huangapple
  • 本文由 发表于 2023年6月15日 17:22:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76481011.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定