我有一个for loop,& while loop,它在每次迭代之后生成一个数据。我想把所有的数据加在一个数据框架中,但是发现它很困难。因为只有从循环中创建的最后一个数据是成功的(可以在下面的图片:输出码中看到)。
这是代码,请建议如何修复它:
df = data.frame(matrix(nrow = 350, ncol = 12))
kol<-1
for (x in 1:350) {
output <- c(paste0(x))
df[,1] = output
}
while (kol <= 223) {
if(kol < 224){
rowd1 <- c(paste("gen ",kol))
}
df[,2] = rowd1
kol = kol+1
}#while
while (kol <= 446) {
if(kol < 447){
rowd2 <- c(paste("gen ",kol))
}
df[,3] = rowd2
kol = kol+1
}
colnames(df) <- c("Kromosom", "A","B","C","D","E","F","G","H","I","J","K")
df因此,我将更新我提出的问题。如果代码变成这样呢:问题:行问题
...
for (x in 1:350) {
output <- c(paste0(x))
df[x,1] = output
}
for (x2 in 1:223) {
output2 <- c(paste("Gen ",x2))
df[1,2:224] = output2
}"#why only the value 223 comes out, like the output in the picture 'row problem' that is -Gen 223-"
...发布于 2021-10-28 18:31:00
解决了
特别感谢@JamesHirschorn,我非常感谢您在解决这个问题上的帮助。感谢那些给出反馈的人。
去做
while远点。为什么不直接用呢?- GuedesBFdf[x, 1] = output为第一列中的每一行设置值代码
df = data.frame(matrix(nrow = 350, ncol = 224))
for (x in 1:350) {
output <- c(paste(x))
df[x,1] = output
}
for (x2 in 2:224) {
output2 <- c(paste("Gen ",x2-1))
df[1,x2] = output2
}
colnames(df) <- c("Kromosom", "A","B","C","D","E","F","G","H","I","J","K", ...)
dfOutput:这里 .So,祝我将来好运
发布于 2021-10-28 14:57:30
对于data.frame df,df[,n]是整个第n列.因此,您将在每一步中设置整个列。在您的代码中,使用
df[x, 1] = output例如,设置单个行的值。
https://stackoverflow.com/questions/69756177
复制相似问题