gpt4 book ai didi

r - 定义基于多个标准的法术 WITH/Missing Data

转载 作者:行者123 更新时间:2023-12-02 07:31:18 25 4
gpt4 key购买 nike

我正在尝试一些我认为在 R 中相当简单的东西,但给我带来了比我讨价还价更多的麻烦。我想使用 R 来根据多个标准定义法术,同时忽略缺失的数据。然后,目标是使用 aggregate 命令计算不同时期的工资均值。我怀疑 TraMineR 包中提供的工具可用于完成此操作,但我很难弄清楚如何操作。

例如,给定以下数据:

Caseid     Year        Unemployed  EmployerID  occID   indID  Wage           
1 1999 0 1 1 1 5.00
1 2000 NA NA NA NA NA
1 2001 NA NA NA NA NA
1 2002 0 1 1 2 6.00
2 1999 0 1 1 1 4.00
2 2000 0 1 1 1 5.00
2 2001 0 1 1 1 6.00
2 2002 1 1 1 1 6.00
3 1999 0 1 1 1 4.00
3 2000 0 3 1 1 5.00
3 2001 0 1 4 1 5.00
3 2002 NA NA NA NA NA
4 1999 0 1 1 1 5.00
4 2000 0 1 1 1 5.00
4 2001 0 1 1 1 7.00
4 2002 0 1 1 1 7.00

我想编写代码,根据就业状况、雇主、职业或行业的变化来定义法术。此外,我想忽略缺失值。鉴于此,正确的代码应该为“Spell”返回以下向量:

Caseid     Year        Unemployed  EmployerID  occID   indID  Wage   Spell         
1 1999 0 1 1 1 5.00 1
1 2000 NA NA NA NA NA 1
1 2001 NA NA NA NA NA 1
1 2002 0 1 1 2 6.00 2
2 1999 0 1 1 1 4.00 1
2 2000 0 1 1 1 5.00 1
2 2001 0 1 1 1 6.00 1
2 2002 1 1 1 1 6.00 2
3 1999 0 1 1 1 4.00 1
3 2000 0 3 1 1 5.00 2
3 2001 0 1 4 1 5.00 3
3 2002 NA NA NA NA NA 3
4 1999 0 1 1 1 5.00 1
4 2000 0 1 1 1 5.00 1
4 2001 0 1 1 1 7.00 1
4 2002 0 1 1 1 7.00 1

最终,我想使用拼写向量来平均个人拼写中的工资。返回以下内容:

Caseid     Year        Unemployed  EmployerID  occID   indID  Wage   Spell  avgWage         
1 1999 0 1 1 1 5.00 1 5.00
1 2000 NA NA NA NA NA 1 5.00
1 2001 NA NA NA NA NA 1 5.00
1 2002 0 1 1 2 6.00 2 6.00
2 1999 0 1 1 1 4.00 1 5.00
2 2000 0 1 1 1 5.00 1 5.00
2 2001 0 1 1 1 6.00 1 5.00
2 2002 1 1 1 1 6.00 2 6.00
3 1999 0 1 1 1 4.00 1 4.00
3 2000 0 3 1 1 5.00 2 5.00
3 2001 0 1 4 1 5.00 3 5.00
3 2002 NA NA NA NA NA 3 5.00
4 1999 0 1 1 1 5.00 1 6.00
4 2000 0 1 1 1 5.00 1 6.00
4 2001 0 1 1 1 7.00 1 6.00
4 2002 0 1 1 1 7.00 1 6.00

这里是调试的数据。需要注意的是,newemp(newemployer)变量与我提供的示例不同,如果值为 1,它应该只开始一个新法术。因此 newemp=1 的一系列 4 年不应该代表一个法术,而是四个不同的法术:

    df <- as.data.frame(structure(list(caseid = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), year = c(1979L,
1980L, 1981L, 1982L, 1983L, 1984L, 1985L, 1986L, 1987L, 1988L,
1989L, 1990L, 1991L, 1992L, 1993L, 1994L, 1996L, 1998L, 2000L,
2002L, 2004L, 2006L, 2008L, 2010L, 1979L, 1980L, 1981L, 1982L,
1983L, 1984L, 1985L, 1986L, 1987L, 1988L, 1989L, 1990L, 1991L,
1992L, 1993L, 1994L, 1996L, 1998L, 2000L, 2002L, 2004L, 2006L,
2008L, 2010L, 1979L, 1980L, 1981L, 1982L, 1983L, 1984L, 1985L,
1986L, 1987L, 1988L, 1989L, 1990L, 1991L, 1992L, 1993L, 1994L,
1996L, 1998L, 2000L, 2002L, 2004L, 2006L, 2008L, 2010L, 1979L,
1980L, 1981L, 1982L, 1983L, 1984L, 1985L, 1986L, 1987L, 1988L,
1989L, 1990L, 1991L, 1992L, 1993L, 1994L, 1996L, 1998L, 2000L,
2002L, 2004L, 2006L, 2008L, 2010L, 1979L, 1980L, 1981L, 1982L,
1983L, 1984L, 1985L, 1986L, 1987L, 1988L, 1989L, 1990L, 1991L,
1992L, 1993L, 1994L, 1996L, 1998L, 2000L, 2002L, 2004L, 2006L,
2008L, 2010L), unemp = c(0, NA, 0, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
0, NA, 0, NA, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0,
0, NA, NA, NA, NA, 1, NA, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, NA, 0,
0, NA, 1, 0, NA, NA, NA, NA, NA, NA, NA, 0, 0, 1, 0, 0, NA, 0,
NA, 0, 0, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA),
newemp = c(NA, NA, 0, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, NA, NA, NA, NA, NA, 0,
1, 1, 1, NA, 1, NA, 0, NA, NA, NA, NA, NA, 1, 0, 0, 1, 0,
1, NA, NA, 0, 0, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 1, 0, 0, 1, NA, 0, 0, NA, 1, 0, NA, NA, NA, NA,
NA, NA, NA, 0, 0, 1, 1, 1, NA, 1, NA, 1, 0, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA), stocc = c(335, NA, 337,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, 337, 337, 337, 337, 337, 23, 386,
23, 23, 337, 389, 23, 337, 23, 337, NA, NA, NA, NA, NA, 276,
276, 276, NA, 383, 376, NA, 383, NA, NA, 383, NA, 447, 468,
155, 468, 373, 188, 243, NA, NA, 243, 22, 277, NA, 22, 469,
NA, NA, NA, NA, 274, NA, NA, NA, 313, 313, 313, 313, 313,
NA, 313, 313, NA, 313, 178, NA, NA, NA, NA, NA, NA, 329,
329, 329, 355, 223, 223, NA, 178, NA, 178, 178, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), stind = c(711, NA,
711, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, 172, 172, 172, 172, 172,
172, 172, 172, 172, 172, 172, 172, 172, 172, 172, NA, NA,
NA, NA, NA, 641, 641, 641, NA, 700, 700, NA, 700, NA, NA,
700, NA, 840, 770, 842, 862, 172, 623, 682, NA, NA, 682,
172, 671, NA, 791, 791, NA, NA, NA, NA, 591, NA, NA, NA,
841, 841, 841, 841, 841, NA, 712, 841, NA, 841, 841, NA,
NA, NA, NA, NA, NA, 850, 850, 850, 932, 850, 841, NA, 841,
NA, 841, 841, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA), lwage = c(2.14335489273071, NA, 2.0160756111145,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, 2.30358457565308, 2.34806489944458,
2.36942100524902, 2.41516351699829, 2.38407301902771, 2.23588967323303,
2.60783195495605, 2.58224511146545, 2.68043231964111, 2.70430994033813,
2.76339650154114, 2.76763892173767, 2.72537922859192, 2.83617949485779,
2.88961029052734, NA, NA, NA, NA, NA, 2.28949975967407, 2.15297079086304,
NA, NA, 2.25023865699768, 2.20731782913208, NA, 2.15908432006836,
NA, NA, 2.17475175857544, NA, 0.0605304837226868, 0.940007209777832,
2.2104697227478, 2.22159194946289, 0.130852773785591, 0.725372314453125,
2.02960777282715, NA, NA, 2.09433007240295, 2.38683438301086,
NA, NA, NA, NA, NA, NA, NA, NA, 1.89671993255615, NA, NA,
NA, 2.63665437698364, 2.7040421962738, 2.79728126525879,
2.72129535675049, 3.03042364120483, NA, 3.02664947509766,
2.7957558631897, NA, 2.86539578437805, 2.20382499694824,
NA, NA, NA, NA, NA, NA, 2.08691358566284, 2.03152418136597,
2.10608339309692, 2.17702174186707, 2.16355276107788, 3.65519332885742,
NA, 3.80884671211243, NA, 3.37032580375671, 3.52329707145691,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA))))

最佳答案

远不如@BrodieG 的data.table 解决方案优雅(这让我相信我真的必须熟悉data.table!),但由于我已经对其进行了编码,所以我不妨在这里提供它。

d <- read.table(text='Caseid     Year        Unemployed  EmployerID  occID   indID  Wage           
1 1999 0 1 1 1 5.00
1 2000 NA NA NA NA NA
1 2001 NA NA NA NA NA
1 2002 0 1 1 2 6.00
2 1999 0 1 1 1 4.00
2 2000 0 1 1 1 5.00
2 2001 0 1 1 1 6.00
2 2002 1 1 1 1 6.00
3 1999 0 1 1 1 4.00
3 2000 0 3 1 1 5.00
3 2001 0 1 4 1 5.00
3 2002 NA NA NA NA NA
4 1999 0 1 1 1 5.00
4 2000 0 1 1 1 5.00
4 2001 0 1 1 1 7.00
4 2002 0 1 1 1 7.00', header=TRUE)


d <- merge(unsplit(
lapply(split(na.omit(d), na.omit(d)$Caseid), function(x) {
cbind(x, Spell=cumsum(!duplicated(x[, 3:6])))
}),
na.omit(d)$Caseid), d, all=TRUE)

d <- merge(d, aggregate(list(avgWage=d$Wage),
list(Caseid=d$Caseid, Spell=d$Spell),
mean, na.rm=TRUE),
all.x=TRUE)

d[order(d$Caseid, d$Year), ]

但请注意,这会为 WageavgWage 返回 NA,其中行包含 NA

关于r - 定义基于多个标准的法术 WITH/Missing Data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21442861/

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