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

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

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

问题

  1. output <- rbind(c("Cat1", "Cat2", "Cat3", "Cat2", "Cat5", "NA"),
  2. c(1, 2, 3, 4, 5, 6),
  3. c(1, 2, 3, 4, 5, 6),
  4. c(1, 2, 3, 4, 5, 6))
  5. colnames(output) <- colnames(data)
英文:
  1. data <- rbind(c(1,2,3,4,5,6),
  2. c(1,2,3,4,5,6),
  3. c(1,2,3,4,5,6))
  4. colnames(data) <- c("A", "B", "C", "D", "E", "F")
  5. category_vector <- cbind(c("A", "B", "C", "D", "E"),
  6. c("Cat1", "Cat2", "Cat3", "Cat2", "Cat5"))
  7. 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:

  1. output <- rbind(c("Cat1", "Cat2", "Cat3", "Cat2", "Cat5", "NA"),
  2. c(1,2,3,4,5,6),
  3. c(1,2,3,4,5,6),
  4. c(1,2,3,4,5,6))
  5. 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

这是一个例子:

  1. library(dplyr)
  2. data <- rbind(c(1,2,3,4,5,6),
  3. c(1,2,3,4,5,6),
  4. c(1,2,3,4,5,6))
  5. colnames(data) <- c("A", "B", "C", "D", "E", "F")
  6. category_vector <- cbind(c("A", "B", "C", "D", "E"),
  7. c("Cat1", "Cat2", "Cat3", "Cat2", "Cat5"))
  8. #colnames(category_vector) <- c("Name", "Category")
  9. # 将data中的所有内容转换为字符型
  10. data <- data %>%
  11. as.data.frame() %>%
  12. mutate(across(everything(), as.character))
  13. category_vector <- as.data.frame(t(category_vector))
  14. names(category_vector) <- category_vector[1, ]
  15. category_vector <- category_vector[-1, ]
  16. category_vector %>%
  17. bind_rows(data)

希望这对你有所帮助。

英文:

Here is one example:

  1. library(dplyr)
  2. data &lt;- rbind(c(1,2,3,4,5,6),
  3. c(1,2,3,4,5,6),
  4. c(1,2,3,4,5,6))
  5. colnames(data) &lt;- c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;, &quot;F&quot;)
  6. category_vector &lt;- cbind(c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;),
  7. c(&quot;Cat1&quot;, &quot;Cat2&quot;, &quot;Cat3&quot;, &quot;Cat2&quot;, &quot;Cat5&quot;))
  8. #colnames(category_vector) &lt;- c(&quot;Name&quot;, &quot;Category&quot;)
  9. # turn everything in data into a character
  10. data &lt;- data |&gt;
  11. as.data.frame() |&gt;
  12. mutate(across(everything(), as.character))
  13. category_vector &lt;- as.data.frame(t(category_vector))
  14. names(category_vector) &lt;- category_vector[1, ]
  15. category_vector &lt;- category_vector[-1, ]
  16. category_vector |&gt;
  17. bind_rows(data)
  18. A B C D E F
  19. 1 Cat1 Cat2 Cat3 Cat2 Cat5 &lt;NA&gt;
  20. 2 1 2 3 4 5 6
  21. 3 1 2 3 4 5 6
  22. 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:

确定