我有一个带有 NA 值的数据框
df <- data.frame("About" = c("Ram","Std 8",NA,NA,NA,"John", "Std 9", NA, NA,NA,NA),
"Questions" = c(NA,NA,"Q1","Q2","Q3",NA,NA,"Q1","Q2","Q3","Q4"),
"Ratings" = c(NA,NA,7,7,7,NA,NA,7,7,7,7), stringsAsFactors = FALSE)
预期输出如下:
expectedOutput <- data.frame("About" = c("Ram","John"),
"Standard" = c("Std 8", "Std 9"),
"Q1" = c(7,7),
"Q2" = c(7,7),
"Q3" = c(7,7),
"Q4" = c(0,7))
我尝试使用reshape
函数来实现这一点
DataTransform <- reshape(df, idvar = "About", v.names = "Ratings", timevar = "Questions", direction = "wide")
任何人都可以帮助我通过 reshape 给定的数据帧来实现预期的输出吗?
提前致谢!!
最佳答案
基本 R
方法,
df2 <- df # Assigning the df into a new one
通过创建新列标准,用最后出现的非 NA
值填充 NA
值,
df2$Standard <- na.omit(df[,1])[cumsum(!is.na(df[,1]))]
同样,取消包含 Std 的名称后,通过将 About 列中的所有值替换为非 NA
值,将出现 finaldf
。
df2[grepl("Std",df2[,1]),1] <- NA
df2[,1] <- na.omit(df2[,1])[cumsum(!is.na(df2[,1]))]
finaldf <- df2[!is.na(df2[,"Ratings"]),]
About Questions Ratings Standard
3 Ram Q1 7 Std 8
4 Ram Q2 7 Std 8
5 Ram Q3 7 Std 8
8 John Q1 7 Std 9
9 John Q2 7 Std 9
10 John Q3 7 Std 9
11 John Q4 7 Std 9
这与您使用 reshape()
函数所做的部分相同。
out <- reshape(finaldf, idvar = "About", v.names = "Ratings", timevar = "Questions", direction = "wide")
out[is.na(out)] <- 0
colnames(out) <- c("About","Standard","Q1","Q2","Q3","Q4")
给予,
About Standard Q1 Q2 Q3 Q4
3 Ram Std 8 7 7 7 0
8 John Std 9 7 7 7 7
关于在 R 中使用 NA 值 reshape 数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62357485/