英文:
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 %>% 
  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
And to check if they speak 1 or 2 languages:
df %>% 
  group_by(Village, Number, Number_1, Number_3) %>% 
  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 <- 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
<sup>Created on 2023-05-15 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论