获取家庭成员数量以及语言上同质的家庭如何?

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

How to get the number of people in the household? and Linguistically homogeneous households?

问题

我需要翻译的部分是:

  • 有一个数据框,我必须获取家庭中的人数语言同质的家庭?
  • 有多少户家庭使用一种语言DAILY_LANG变量是语言代码。我需要找出有多少家庭使用一种语言,以及有多少家庭使用两种语言。
英文:

There is a dataframe I have to get the Number of people in the household and and Linguistically homogeneous households?

  1. df <- data.frame(
  2. Village = c(rep("1", "30")),
  3. Number = c(33, 33, 33, 33, 33, 33, 33, 1, 1, 30, 30, 30, 30, 30, 30, 30,
  4. 31, 31, 31, 31, 36, 36, 36, 36, 62, 62, 62, 62, 69, 69),
  5. Number_1 = c(183, 183, 183, 183, 183, 183, 183, 151, 151, 255, 255, 255, 255, 255, 255,
  6. 255, 31, 31, 31, 31, 111, 111, 111, 111, 287, 287, 287,287, 219, 219),
  7. Number_3 = c(137, 137, 137, 137, 137, 137, 137, 113, 113, 191, 191, 191, 191, 191, 191,
  8. 191, 23, 23, 23, 23, 83, 83, 83, 83, 215, 215, 215, 215, 164, 164),
  9. PERSNUM = c(1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 1, 2, 3, 4, 5, 6,
  10. 1, 2, 3, 1, 2, 3, 4, 5, 1, 2, 3, 4, 1, 2),
  11. DAILY_LANG = c(33, 33, 33, 33, 33, 33, 33, 1, 1, 1, 1, 1, 1, 0, 11,
  12. 11, 11, 11, 11, 11, 0, 0, 11, 11, 11, 11, 11, 11, 11, 11))

How many household speak in one Language? the DAILY_LANG variable is the language code. I need find how many of families speak in one language and how many speak in 2 language.

Note: this question How to get the number of people in the household? answered. just need on find the ** Linguistically homogeneous households?**

Much appreciate for your answer

答案1

得分: 1

以下是您要翻译的内容:

按列分组,检查最大的PERSNUM是否为3或4:

  1. library(dplyr)
  2. df %>%
  3. group_by(Village, Number, Number_1, Number_3) %>%
  4. filter(max(PERSNUM) %in% c(3, 4))
  5. # A tibble: 8 × 5
  6. # Groups: Village, Number, Number_1, Number_3 [2]
  7. # Village Number Number_1 Number_3 PERSNUM
  8. # <chr> <dbl> <dbl> <dbl> <dbl>
  9. # 1 1 31 31 23 1
  10. # 2 1 31 31 23 2
  11. # 3 1 31 31 23 3
  12. # 4 1 31 31 23 1
  13. # 5 1 62 287 215 1
  14. # 6 1 62 287 215 2
  15. # 7 1 62 287 215 3
  16. # 8 1 62 287 215 4

然后检查他们是否会说1或2种语言:

  1. df %>%
  2. group_by(Village, Number, Number_1, Number_3) %>%
  3. filter(length(unique(DAILY_LANG)) %in% c(1, 2))
英文:

Group by columns, check if the maximum PERSNUM is 3 or 4:

  1. library(dplyr)
  2. df %&gt;%
  3. group_by(Village, Number, Number_1, Number_3) %&gt;%
  4. filter(max(PERSNUM) %in% c(3, 4))
  5. # A tibble: 8 &#215; 5
  6. # Groups: Village, Number, Number_1, Number_3 [2]
  7. # Village Number Number_1 Number_3 PERSNUM
  8. # &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
  9. # 1 1 31 31 23 1
  10. # 2 1 31 31 23 2
  11. # 3 1 31 31 23 3
  12. # 4 1 31 31 23 1
  13. # 5 1 62 287 215 1
  14. # 6 1 62 287 215 2
  15. # 7 1 62 287 215 3
  16. # 8 1 62 287 215 4

And to check if they speak 1 or 2 languages:

  1. df %&gt;%
  2. group_by(Village, Number, Number_1, Number_3) %&gt;%
  3. filter(length(unique(DAILY_LANG)) %in% c(1, 2))

答案2

得分: -1

以下是您要翻译的内容:

"不清楚您想要计算哪一列。但是这里是您问题的一般回答:

  1. library(tidyverse)
  2. df <- data.frame(
  3. foo = c(rep("a",5), rep("b", 5)),
  4. bar = c(1,1,1,3,3,1,1,3,3,3)
  5. )
  6. print(df)
  7. #> foo bar
  8. #> 1 a 1
  9. #> 2 a 1
  10. #> 3 a 1
  11. #> 4 a 3
  12. #> 5 a 3
  13. #> 6 b 1
  14. #> 7 b 1
  15. #> 8 b 3
  16. #> 9 b 3
  17. #> 10 b 3
  18. result <- df %>%
  19. group_by(foo) %>%
  20. summarise(count = sum(bar == 3))
  21. print(result)
  22. #> # A tibble: 2 × 2
  23. #> foo count
  24. #> <chr> <int>
  25. #> 1 a 2
  26. #> 2 b 3

创建于2023年5月15日,使用reprex v2.0.2"

英文:

It's not clear which column you want to count. But here is a general answer to your problem:

  1. library(tidyverse)
  2. df &lt;- data.frame(
  3. foo = c(rep(&quot;a&quot;,5), rep(&quot;b&quot;, 5)),
  4. bar = c(1,1,1,3,3,1,1,3,3,3)
  5. )
  6. print(df)
  7. #&gt; foo bar
  8. #&gt; 1 a 1
  9. #&gt; 2 a 1
  10. #&gt; 3 a 1
  11. #&gt; 4 a 3
  12. #&gt; 5 a 3
  13. #&gt; 6 b 1
  14. #&gt; 7 b 1
  15. #&gt; 8 b 3
  16. #&gt; 9 b 3
  17. #&gt; 10 b 3
  18. result &lt;- df %&gt;%
  19. group_by(foo) %&gt;%
  20. summarise(count = sum(bar == 3))
  21. print(result)
  22. #&gt; # A tibble: 2 &#215; 2
  23. #&gt; foo count
  24. #&gt; &lt;chr&gt; &lt;int&gt;
  25. #&gt; 1 a 2
  26. #&gt; 2 b 3

<sup>Created on 2023-05-15 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年5月15日 14:23:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76251361.html
匿名

发表评论

匿名网友

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

确定