在R中分组两列并计数

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

group two columns and count in R

问题

我正在尝试使用数据框来进行简单的计数。例如,如果数据框如下所示:

  1. > df
  2. ID lable col1 col2
  3. 1 Buckinghamshire 1 A A
  4. 2 Cornwall and Isles of Scilly 2 B B
  5. 3 Devon 1 A A
  6. 4 Dunfermline 2 C C
  7. 5 Humberside 2 C C
  8. 6 Inner London X A A
  9. 7 Kent X A A
  10. 8 Kirkcaldy 1 C C
  11. 9 Lancashire 1 B B
  12. 10 Not known/missing 2 C C

期望的输出如下:

  1. > df2
  2. name group 1 2 X
  3. 1 col1 A 4647 4858 108
  4. 1 col1 B 120456 146864 3502
  5. 1 col1 C 258 53 111
  6. 2 col2 A 12247 1202 66
  7. 2 col2 B 4585 258 1
  8. 2 col2 C 32158 15426 477

我该如何解决这个问题以获得期望的输出?

英文:

I am trying to do a simple counts with dataframe
For example, if the df is as below:

  1. > df
  2. ID lable col1 col2
  3. 1 Buckinghamshire 1 A A
  4. 2 Cornwall and Isles of Scilly 2 B B
  5. 3 Devon 1 A A
  6. 4 Dunfermline 2 C C
  7. 5 Humberside 2 C C
  8. 6 Inner London X A A
  9. 7 Kent X A A
  10. 8 Kirkcaldy 1 C C
  11. 9 Lancashire 1 B B
  12. 10 Not known/missing 2 C C

Desire output

  1. > df2
  2. name group 1 2 X
  3. 1 col1 A 4647 4858 108
  4. 1 col1 B 120456 146864 3502
  5. 1 col1 C 258 53 111
  6. 2 col2 A 12247 1202 66
  7. 2 col2 B 4585 258 1
  8. 2 col2 C 32158 15426 477

How can I solve this so I can get desire output

答案1

得分: 0

这是您提供的代码的翻译:

  1. 问题不清晰。
  2. lines="
  3. ID, 标签, 列1, 列2
  4. 白金汉郡 , 1 ,A,A
  5. 康沃尔和西西里群岛 , 2 ,B,B
  6. 德文 , 1 ,A,A
  7. 邓弗姆林 , 2 ,C,C
  8. 亨伯赛德 , 2 ,C,C"
  9. con <- textConnection(lines)
  10. data <- read.csv(con)
  11. data$col2=trimws(data$col2)
  12. library(tidyr)
  13. library(dplyr)
  14. temp=gather(data, key="name", value="group",3:4)
  15. temp2=temp[,c(2,3,4)] %>%
  16. group_by(name, group, label) %>% summarise(count=n())
  17. spread(temp2, key="label", value="count")

结果:

  1. 名称 组别 `1` `2`
  2. <chr> <chr> <int> <int>
  3. 1 1 A 2 NA
  4. 2 1 B NA 1
  5. 3 1 C NA 2
  6. 4 2 A 2 NA
  7. 5 2 B NA 1
  8. 6 2 C NA 2
英文:

The question is not clear.

  1. lines=&quot;
  2. ID, label, col1, col2
  3. Buckinghamshire , 1 ,A,A
  4. Cornwall and Isles of Scilly , 2 ,B,B
  5. Devon , 1 ,A,A
  6. Dunfermline , 2 ,C,C
  7. Humberside , 2 ,C,C&quot;
  8. con &lt;- textConnection(lines)
  9. data &lt;- read.csv(con)
  10. data$col2=trimws(data$col2)
  11. library(tidyr)
  12. library(dplyr)
  13. temp=gather(data, key=&quot;name&quot;, value=&quot;group&quot;,3:4)
  14. temp2=temp[,c(2,3,4)] %&gt;%
  15. group_by(name, group, label) %&gt;% summarise(count=n())
  16. spread(temp2, key=&quot;label&quot;, value=&quot;count&quot;)

Result:

  1. name group `1` `2`
  2. &lt;chr&gt; &lt;chr&gt; &lt;int&gt; &lt;int&gt;
  3. 1 col1 A 2 NA
  4. 2 col1 B NA 1
  5. 3 col1 C NA 2
  6. 4 col2 A 2 NA
  7. 5 col2 B NA 1
  8. 6 col2 C NA 2

huangapple
  • 本文由 发表于 2023年2月16日 14:57:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75468784.html
匿名

发表评论

匿名网友

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

确定