我有以下数据框:
Variable1 Variable2 Variable3 Variable4
number1 number2 number3 number4
gene1 gene2 gene4 gene5
gene3
我希望在文本文件中打印以下重新排列:
gene1 number1
gene2 number2
gene3 number2
gene4 number3
gene5 number4
我尝试编写以下循环函数:
print <- function(dataframe){
for (i in 2:nrow(dataframe))
print(i)
}
这样我就可以使用apply:
lapply(myDataFrame)
但它不起作用。基本上,我想为一个将应用于每列的向量编写一个循环函数。但是,它不起作用。我很感激任何指针。
答案 0 :(得分:0)
您可以按照@RomanLuštrik的建议尝试melt
。如果dat
是数据集
library(reshape2)
res <- melt(as.matrix(setNames(dat[-1,], dat[1,])))[,-1]
res[res$value!='',2:1]
# value Var2
#1 gene1 number1
#3 gene2 number2
#4 gene3 number2
#5 gene4 number3
#7 gene5 number4
或使用stack
subset(stack(setNames(dat[-1,], dat[1,])), values!='')
# values ind
#1 gene1 number1
#3 gene2 number2
#4 gene3 number2
#5 gene4 number3
#7 gene5 number4
dat <- structure(list(Variable1 = c("number1", "gene1", ""), Variable2 = c("number2",
"gene2", "gene3"), Variable3 = c("number3", "gene4", ""), Variable4 = c("number4",
"gene5", "")), .Names = c("Variable1", "Variable2", "Variable3",
"Variable4"), class = "data.frame", row.names = c(NA, -3L))
假设我将dat
列转换为factors
dat[] <- lapply(dat, as.factor)
head(melt(as.matrix(setNames(dat[-1,], dat[1,])))[,-1],2)
# Var2 value
#1 3 gene1
#2 3
因此,最好将列转换为'character'类
dat[] <- lapply(dat, as.character)
head(melt(as.matrix(setNames(dat[-1,], dat[1,])))[,-1],2)
# Var2 value
#1 number1 gene1
#2 number1