gpt4 book ai didi

r - 在 R 中处理具有相同 Id(键)列值的多行

转载 作者:行者123 更新时间:2023-12-04 10:37:34 24 4
gpt4 key购买 nike

我正在处理人口普查数据。数据集的样子是:

Household-Id    Member-Type    Education    Birth
1 Father 12 1955
1 Mother 16 1963
1 Child 16 1986
1 Child 12 1995
2 Father 12 1950
2 Mother 9 1955
2 Child 18 1982
2 Child 14 1985
2 Child 16 1975
3 Father 16 1962
3 Mother 14 1965
3 Child 16 1990

我希望它看起来像:

Household-Id    Member-Type    Education    Birth    Mother-Education    Birth-Order 
1 Father 12 1955
1 Mother 16 1963
1 Child 16 1986 16 1
1 Child 12 1995 16 2
2 Father 12 1950
2 Mother 9 1955
2 Child 18 1982 9 1
2 Child 14 1985 9 2
2 Child 16 1975 9 3
3 Father 16 1962
3 Mother 14 1965
3 Child 16 1990 14 1

据我所知,R 不像 Java 或 C 语言那样支持循环操作,而且我真的不知道如何做到这一点!

最佳答案

您从哪里听说 R 不支持循环?确实如此——这种特殊情况听起来最适合 data.table(内部使用循环)

install.packages("data.table")
library(data.table)
dat = as.data.table(YourDataFrame)

dat[Member.Type == "Child", Birth_Order:=rank(Birth) ,by=Household.Id]
dat[, MotherEducation := Education[Member.Type=="Mother"] , by=Household.Id]
dat[Member.Type != "Child", MotherEducation := NA]
dat
# Household.Id Member.Type Education Birth MotherEducation Birth_Order
# 1: 1 Father 12 1955 NA NA
# 2: 1 Mother 16 1963 NA NA
# 3: 1 Child 16 1986 16 1
# 4: 1 Child 12 1995 16 2
# 5: 2 Father 12 1950 NA NA
# 6: 2 Mother 9 1955 NA NA
# 7: 2 Child 18 1982 9 2
# 8: 2 Child 14 1985 9 3
# 9: 2 Child 16 1975 9 1
# 10: 3 Father 16 1962 NA NA
# 11: 3 Mother 14 1965 NA NA
# 12: 3 Child 16 1990 14 1

关于r - 在 R 中处理具有相同 Id(键)列值的多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32341424/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com