在 R 中使用 NA 值 reshape 数据框

标签 r dataframe reshape

我有一个带有 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/

相关文章:

python - 如何获取 Pandas 系列中小数点后的最大位数

将数组 reshape 为 data.frame

r - 带有 "["参数的 'missing' 的 S4 文档

python - 查找数据框是否是另一个数据框的子集,同时忽略索引

将宽格式 reshape 为多列长格式

python - 如何使某些 pandas 列值默认为不同列但同一行中的另一个值?

python - Keras 输入形状,输入列表的简单数组

arrays - MATLAB:将 N x 4 数组置换并 reshape 为 2 x 2 x N 数组

r - 将矩阵与R中的向量等同是什么意思

r - 在 lyx 中使用 tikzDevice 不起作用