将 N 列的数据框转换为两 'stacked' 列的数据框
Convert data frame of N columns into a data frame of two 'stacked' columns
你好 Stack 社区。
我正在从事网络分析工作,并且有一个数据重塑问题。
我的原始数据以一系列列的形式出现,每一列都是 "source" 和 "target" 对。最终的数据框需要由两列 "source" 和 "target" 组成。请注意,这些对是交错的,因为它们的源和目标在定向网络中是链接的。 (有关所需输出,请参阅代码示例中的 final_output)
我创建了一个非常 hacky 的方法来生成我需要的输出(见下面的代码),但它不能容纳不同数量的列,除非我添加变量和诸如此类的东西。另外,请注意,在某些情况下,列对的数量将是奇数,即一个 "source",数据框末尾没有 "target"。在这种情况下,缺少的 "target" 列是使用 NA 创建的。
我觉得有一种无需所有手工操作即可轻松制作的方法。我一直在寻找和寻找,但没有发现任何东西。非常感谢您的帮助。
蒂姆
# Create example DF
mydf <- data.frame(id = 1:6, varA = "A",
varB = "B",
varC = "C",
varD = "D",
varE = "E",
varF = "F")
#Remove the ID value for DF build. This variable is not in real DF
mydf$id <-NULL
#Begin inelegant hack.
#Please note: the incoming DF has an indeterminate number of columns that vary with project
counter <-ncol(mydf)
for (i in 1:counter){
t1 <-mydf[(counter-counter+1):(counter-counter+2)]
t2 <-mydf[(counter-counter+2):(counter-counter+3)]
t3 <-mydf[(counter-counter+3):(counter-counter+4)]
t4 <-mydf[(counter-counter+4):(counter-counter+5)]
t5 <-mydf[(counter-counter+5):(counter-counter+6)]
}
#Rename for the rbind
names(t1) <-c("Source", "Target")
names(t2) <-c("Source", "Target")
names(t3) <-c("Source", "Target")
names(t4) <-c("Source", "Target")
names(t5) <-c("Source", "Target")
#This is the shape I need but the process is super manual and does not accommodate differing numbers of columns.
final_output <-rbind(t1,t2,t3,t4,t5)
如果我没理解错的话,你可以使用 unlist
并手动创建你的 data.frame
:
mydf[] <- lapply(mydf, as.character) # Convert factors to characters
final_output <- data.frame(Source = unlist(mydf[-length(mydf)]),
Target = unlist(mydf[-1]))
head(final_output, 15)
# Source Target
# varA1 A B
# varA2 A B
# varA3 A B
# varA4 A B
# varA5 A B
# varA6 A B
# varB1 B C
# varB2 B C
# varB3 B C
# varB4 B C
# varB5 B C
# varB6 B C
# varC1 C D
# varC2 C D
# varC3 C D
你好 Stack 社区。
我正在从事网络分析工作,并且有一个数据重塑问题。
我的原始数据以一系列列的形式出现,每一列都是 "source" 和 "target" 对。最终的数据框需要由两列 "source" 和 "target" 组成。请注意,这些对是交错的,因为它们的源和目标在定向网络中是链接的。 (有关所需输出,请参阅代码示例中的 final_output)
我创建了一个非常 hacky 的方法来生成我需要的输出(见下面的代码),但它不能容纳不同数量的列,除非我添加变量和诸如此类的东西。另外,请注意,在某些情况下,列对的数量将是奇数,即一个 "source",数据框末尾没有 "target"。在这种情况下,缺少的 "target" 列是使用 NA 创建的。
我觉得有一种无需所有手工操作即可轻松制作的方法。我一直在寻找和寻找,但没有发现任何东西。非常感谢您的帮助。
蒂姆
# Create example DF
mydf <- data.frame(id = 1:6, varA = "A",
varB = "B",
varC = "C",
varD = "D",
varE = "E",
varF = "F")
#Remove the ID value for DF build. This variable is not in real DF
mydf$id <-NULL
#Begin inelegant hack.
#Please note: the incoming DF has an indeterminate number of columns that vary with project
counter <-ncol(mydf)
for (i in 1:counter){
t1 <-mydf[(counter-counter+1):(counter-counter+2)]
t2 <-mydf[(counter-counter+2):(counter-counter+3)]
t3 <-mydf[(counter-counter+3):(counter-counter+4)]
t4 <-mydf[(counter-counter+4):(counter-counter+5)]
t5 <-mydf[(counter-counter+5):(counter-counter+6)]
}
#Rename for the rbind
names(t1) <-c("Source", "Target")
names(t2) <-c("Source", "Target")
names(t3) <-c("Source", "Target")
names(t4) <-c("Source", "Target")
names(t5) <-c("Source", "Target")
#This is the shape I need but the process is super manual and does not accommodate differing numbers of columns.
final_output <-rbind(t1,t2,t3,t4,t5)
如果我没理解错的话,你可以使用 unlist
并手动创建你的 data.frame
:
mydf[] <- lapply(mydf, as.character) # Convert factors to characters
final_output <- data.frame(Source = unlist(mydf[-length(mydf)]),
Target = unlist(mydf[-1]))
head(final_output, 15)
# Source Target
# varA1 A B
# varA2 A B
# varA3 A B
# varA4 A B
# varA5 A B
# varA6 A B
# varB1 B C
# varB2 B C
# varB3 B C
# varB4 B C
# varB5 B C
# varB6 B C
# varC1 C D
# varC2 C D
# varC3 C D