我想以一种方式转换我的数据集,对于每个原始行,其中的所有值都成为新行中的第一个,同时保留其他值。
我不确定这个解释是否清楚,所以这里有一个例子: 让我们说我的数据框看起来像这样:
V1 V2 V3 V4 V5 V6
valve valves VALVE VALVES Valve Valves
vaporizer vaporizers Vaporizer Vaporizers NA NA
我想要
V1 V2 V3 V4 V5 V6
valve valves VALVE VALVES Valve Valves
valves valve VALVE VALVES Valve Valves
VALVE valves valve VALVES Valve Valves
VALVES valves VALVE valve Valve Valves
Valve valves VALVE VALVES valve Valves
Valves valves VALVE VALVES Valve valve
vaporizer vaporizers Vaporizer Vaporizers NA NA
vaporizers vaporizer Vaporizer Vaporizers NA NA
Vaporizer vaporizers vaporizer Vaporizers NA NA
Vaporizers vaporizers Vaporizer vaporizer NA NA
这意味着考虑到存在NA的事实(并且当存在NA时不创建新行。)
自2天以来,我一直在努力解决这个问题,因此非常感谢任何帮助!
非常感谢
答案 0 :(得分:0)
apply
的输出是一个列表,因此我使用do.call(rbind,listobject)
将其拼接回来:
# data
dat <- data.frame(V1 = c("valve","vaporizer"), V2 = c("valves","vaporizers"),
V3 = c("VALVE","Vaporizer"), V4 = c("VALVES","Vaporizers"),
V5 = c("Valve",NA), V6 = c("Valves",NA),
stringsAsFactors = F)
# function
musicalchairs <- function(x){
y <- as.data.frame(matrix(NA, nrow = sum(!is.na(x)), ncol = length(x)))
y[1,] <- x
for(i in 1:length(x)){
if(!is.na(x[i])){
y[i+1,] <- c(y[1,i+1], y[i,-(i+1)])
}
}
y[1:sum(!is.na(x)),]
}
# desired output
do.call(rbind, apply(dat, 1, musicalchairs))
如果您有疑问,请告诉我。