我对R很新。我有一个大数据集,我想为每个人重复一个特定的单元格。见下面的一个子集:
dfX <- data.frame("picode" = c(1,1,1,1,1,1,2,2,2,2,2,2,3,3,4,4,4,4,4,5,6,6,6,6),
"Age" = c(0,7,14,21,28,30,0,7,14,21,28,30,0,8,0,8,15,21,29,0,0,5,7,14),
"IUGRsc1" = c(0.12,NA,NA,NA,NA,NA,0.26,NA,NA,NA,NA,NA,0.15,NA,0.19,NA,NA,NA,NA,0.3,0.23,NA,NA,NA))
我想对每个人重复IUGRsc1,即:
dfX2 <- data.frame("picode" = c(1,1,1,1,1,1,2,2,2,2,2,2,3,3,4,4,4,4,4,5,6,6,6,6),
"Age" = c(0,7,14,21,28,30,0,7,14,21,28,30, 0,8,0,8,15,21,29,0,0,5,7,14),
"IUGRsc1" = c(0.12,0.12,0.12,0.12,0.12,0.12,0.26,0.26,0.26,0.26,0.26,0.26,0.15,0.15,0.19,0.19,0.19,0.19,0.19,0.3,0.23,0.23,0.23,0.23))
我尝试使用for
循环,但无法弄清楚如何让R了解每个人重复的细胞...有什么建议吗?
答案 0 :(得分:1)
您可以使用tidyr::fill()
填写缺失值。
使用上一个条目填充缺失值。这在不重复值的通用输出格式中很有用,每次更改时都会记录它们。
library(tidyverse)
identical(dfX2, fill(dfX, IUGRsc1))
[1] TRUE
答案 1 :(得分:0)
您可以使用rep
和times
选项
each
例如。
IUGRsc1 = rep(x=c(0.12,0.26,0.15.....),times=3,each=6)
答案 2 :(得分:0)
您需要创建两个数据框并使用合并功能。它类似于excel中的v-lookup和SQL中的左连接。
df1 = data.frame( picode=c(1,1,1,1,1,1,2,2,2,2,2,2,3,3,4,4,4,4,4,5,6,6,6,6),
Age=c(0,7,14,21,28,30,0,7,14,21,28,30, 0,8,0,8,15,21,29,0,0,5,7,14))
df2 = data.frame(picode = c(1,2,3,4,5,6),IUGRsc1=c(0.12,0.26,0.15,0.19,0.3,0.23))
df = merge(df1, df2, all.x = TRUE, by.x = 'picode', by.y = 'picode', sort = FALSE)
head(df)
谢谢