啊啊啊啊啊吖

2019-03-15   阅读量: 146

数据分析师 Python数据分析

为缺少的年份添加值为“0”的行

我有一个问题与已发布的其他问题有些类似,但在仔细查看了几个帖子之后,我无法让代码工作。任何帮助将非常感激。

我的数据框看起来像这样:

'data.frame': 501 obs. of 5 variables:

$ Tattoo.MUM : Factor w/ 250 levels "1004","1007",..: 76 76 76 81 81 81 85 85 85 85 ...

$ OffspringMUMs: int 4 4 4 4 4 4 11 11 11 11 ...

$ YearBIRTH.CUB: int 1988 1990 1991 1988 1991 2007 1989 1991 1992 1993 ...

$ YearBIRTH.MUM: int 1991 1991 NA NA NA NA 1987 1987 1987 1987 ...

$ OFFSpYR : int 2 1 1 1 2 1 1 4 3 3 ...

这里有几行:

structure(list(Tattoo.MUM = structure(c(6L, 6L, 6L, 6L, 7L, 7L,

7L, 8L, 9L, 11L, 11L, 11L, 11L, 5L, 1L, 4L, 2L, 3L, 3L, 10L,

10L, 10L, 10L, 10L), .Label = c("10454", "1045A", "1045X", "12392",

"1601", "22", "27", "29", "41", "424X", "60"), class = "factor"),

OffspringMUMs = c(11L, 11L, 11L, 11L, 5L, 5L, 5L, 1L, 3L,

7L, 7L, 7L, 7L, 1L, 2L, 1L, 1L, 4L, 4L, 6L, 6L, 6L, 6L, 6L

), YearBIRTH.CUB = c(1989L, 1991L, 1992L, 1993L, 1990L, 1991L,

1993L, 1989L, 1988L, 1988L, 1989L, 1991L, 1994L, 2015L, 2012L,

2015L, 2005L, 2009L, 2010L, 1996L, 1998L, 2000L, 2001L, 2006L

), YearBIRTH.MUM = c(1987L, 1987L, 1987L, 1987L, NA, NA,

NA, NA, NA, 1987L, 1987L, 1987L, 1987L, NA, NA, NA, NA, 2005L,

2005L, 1994L, 1994L, 1994L, 1994L, 1994L), OFFSpYR = c(1L,

4L, 3L, 3L, 1L, 1L, 3L, 1L, 3L, 3L, 1L, 2L, 1L, 1L, 2L, 1L,

1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L)), .Names = c("Tattoo.MUM",

"OffspringMUMs", "YearBIRTH.CUB", "YearBIRTH.MUM", "OFFSpYR"), class = "data.frame", row.names = c(NA,

-24L))

我想在Tattoo.MUM中为所有缺失年份(YearBIRTH.CUB)添加新行,保持其余值相同并向OFFSpYR添加“0”。

像这样:

structure(list(Tattoo.MUM = structure(c(6L, 6L, 6L, 6L, 6L, 7L,

7L, 7L, 7L, 8L, 9L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 5L, 1L,

4L, 2L, 3L, 3L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,

10L, 10L), .Label = c("10454", "1045A", "1045X", "12392", "1601",

"22", "27", "29", "41", "424X", "60"), class = "factor"), OffspringMUMs = c(11L,

11L, 11L, 11L, 11L, 5L, 5L, 5L, 5L, 1L, 3L, 7L, 7L, 7L, 7L, 7L,

7L, 7L, 1L, 2L, 1L, 1L, 4L, 4L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,

6L, 6L, 6L), YearBIRTH.CUB = c(1989L, 1990L, 1991L, 1992L, 1993L,

1990L, 1991L, 1992L, 1993L, 1989L, 1988L, 1988L, 1989L, 1990L,

1991L, 1992L, 1993L, 1994L, 2015L, 2012L, 2015L, 2005L, 2009L,

2010L, 1996L, 1997L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L,

2004L, 2005L, 2006L), YearBIRTH.MUM = c(1987L, 1987L, 1987L,

1987L, 1987L, NA, NA, NA, NA, NA, NA, 1987L, 1987L, 1987L, 1987L,

1987L, 1987L, 1987L, NA, NA, NA, NA, 2005L, 2005L, 1994L, 1994L,

1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 1994L

), OFFSpYR = c(1L, 0L, 4L, 3L, 3L, 1L, 1L, 0L, 3L, 1L, 3L, 3L,

1L, 0L, 2L, 0L, 0L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 0L, 1L, 0L,

1L, 2L, 0L, 0L, 0L, 0L, 1L)), .Names = c("Tattoo.MUM", "OffspringMUMs",

"YearBIRTH.CUB", "YearBIRTH.MUM", "OFFSpYR"), class = "data.frame", row.names = c(NA,

解决办法:我以前没用过complete(),但文档建议:

df1 <- pedMUM %>%

complete(Tattoo.MUM, YearBIRTH.CUB, fill=list(OFFSpYR=0))

要填写其他列,请执行以下操作:

df1 <- pedMUM %>%

complete(nesting(Tattoo.MUM, YearBIRTH.MUM),

YearBIRTH.CUB=full_seq(YearBIRTH.CUB, 1),

fill=list(OffspringMUMs=0, OFFSpYR=0))

15.5086 1 3 关注作者 收藏

评论(0)


暂无数据

推荐课程

推荐帖子