我有一个带有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")
谁能通过重塑给定的数据框来帮助我实现预期的输出?
提前致谢!!
一种base R
方法
df2 <- df # Assigning the df into a new one
要通过创建新列Standard来NA
用最后出现的值填充值,non NA
df2$Standard <- na.omit(df[,1])[cumsum(!is.na(df[,1]))]
类似地,在取消名称后,包括Std的名称将通过NA
在About列中用非值替换所有值而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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句