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

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

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?

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

library(dplyr)

df %>%
  group_by(Village, Number, Number_1, Number_3) %>%
  filter(max(PERSNUM) %in% c(3, 4))
# A tibble: 8 × 5
# Groups:   Village, Number, Number_1, Number_3 [2]
#   Village Number Number_1 Number_3 PERSNUM
#   <chr>    <dbl>    <dbl>    <dbl>   <dbl>
# 1 1           31       31       23       1
# 2 1           31       31       23       2
# 3 1           31       31       23       3
# 4 1           31       31       23       1
# 5 1           62      287      215       1
# 6 1           62      287      215       2
# 7 1           62      287      215       3
# 8 1           62      287      215       4

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

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

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

library(dplyr)

df %&gt;% 
  group_by(Village, Number, Number_1, Number_3) %&gt;% 
  filter(max(PERSNUM) %in% c(3, 4))
# A tibble: 8 &#215; 5
# Groups:   Village, Number, Number_1, Number_3 [2]
#   Village Number Number_1 Number_3 PERSNUM
#   &lt;chr&gt;    &lt;dbl&gt;    &lt;dbl&gt;    &lt;dbl&gt;   &lt;dbl&gt;
# 1 1           31       31       23       1
# 2 1           31       31       23       2
# 3 1           31       31       23       3
# 4 1           31       31       23       1
# 5 1           62      287      215       1
# 6 1           62      287      215       2
# 7 1           62      287      215       3
# 8 1           62      287      215       4

And to check if they speak 1 or 2 languages:

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

答案2

得分: -1

以下是您要翻译的内容:

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

library(tidyverse)

df <- data.frame(
  foo = c(rep("a",5), rep("b", 5)),
  bar = c(1,1,1,3,3,1,1,3,3,3)
)

print(df)
#>    foo bar
#> 1    a   1
#> 2    a   1
#> 3    a   1
#> 4    a   3
#> 5    a   3
#> 6    b   1
#> 7    b   1
#> 8    b   3
#> 9    b   3
#> 10   b   3

result <- df %>%
  group_by(foo) %>%
  summarise(count = sum(bar == 3))

print(result)
#> # A tibble: 2 × 2
#>   foo   count
#>   <chr> <int>
#> 1 a         2
#> 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:

library(tidyverse)

df &lt;- data.frame(
  foo = c(rep(&quot;a&quot;,5), rep(&quot;b&quot;, 5)),
  bar = c(1,1,1,3,3,1,1,3,3,3)
)

print(df)
#&gt;    foo bar
#&gt; 1    a   1
#&gt; 2    a   1
#&gt; 3    a   1
#&gt; 4    a   3
#&gt; 5    a   3
#&gt; 6    b   1
#&gt; 7    b   1
#&gt; 8    b   3
#&gt; 9    b   3
#&gt; 10   b   3

result &lt;- df %&gt;%
  group_by(foo) %&gt;%
  summarise(count = sum(bar == 3))

print(result)
#&gt; # A tibble: 2 &#215; 2
#&gt;   foo   count
#&gt;   &lt;chr&gt; &lt;int&gt;
#&gt; 1 a         2
#&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:

确定