如何使用数据框中的值作为 R 中的 for 循环迭代器?
How do you use a value from a data frame as the for loop iterator in R?
我试图将数据框中的数值设置为 R 中 for 循环中的迭代器条件,但我无法让它工作。
脚本的思路是这样的:
- 遍历数据框中的行
- 对于每一行,根据行中的值指示循环遍历一个块
例如,我有一个名为 events
的数据框,如下所示:
Row Event Years
1 1 10
2 2 7
我想运行下面的循环来执行上面的脚本:
for (i in 1:nrow(events)){
for(j in 1:events[i,2]{
print(paste('The event number is',i,'and the year number is',j))
}
}
我正在寻找的结果如下所示:
[1] "The event number is 1 and the year number is 1."
[1] "The event number is 1 and the year number is 2."
[1] "The event number is 1 and the year number is 3."
[1] "The event number is 1 and the year number is 4."
[1] "The event number is 1 and the year number is 5."
[1] "The event number is 1 and the year number is 6."
[1] "The event number is 1 and the year number is 7."
[1] "The event number is 1 and the year number is 8."
[1] "The event number is 1 and the year number is 9."
[1] "The event number is 1 and the year number is 10."
[1] "The event number is 2 and the year number is 1."
[1] "The event number is 2 and the year number is 2."
[1] "The event number is 2 and the year number is 4."
[1] "The event number is 2 and the year number is 5."
[1] "The event number is 2 and the year number is 6."
[1] "The event number is 2 and the year number is 7."
但我得到的结果是:
[1] "The event number is 1 and the year number is 1."
[1] "The event number is 2 and the year number is 1."
[1] "The event number is 2 and the year number is 2."
出于测试目的,我尝试了以下代码,它按我希望的方式工作...
for (i in 1:nrow(events)){
for(j in 1:10){
print(paste('The event number is',i,'and the year number is',j))
}
}
...所以我尝试了各种方法来动态复制 10
值。
我是 R 新手,但我不是编程新手;我已经尝试了所有方法,从调用索引的方式到值的类型,再到使用 dim()
设置和获取维度,再到下面列出的其他方法:
1:as.numeric(events[i,2]
1:events$Years[i]
1:as.integer(events[i,2])
1:as.factor(events[i,2])
我认为我将迭代器视为单个值(如整数)而不是像因子或向量这样的 R 构造,这就是我遗漏的地方。我也读过有关使用 apply
函数解决此类问题的信息,但我不熟悉它的用法,所以我想先尝试解决 for
循环。
有谁知道我这里有什么问题吗?我一整天都在为这个烦恼!
在此先感谢您的帮助。
尝试:
df <- read.table(header = TRUE, text = "Row Event Years
1 1 10
2 2 7")
Count <- do.call("c", lapply(df$Years, function(x) seq(1, x)))
df <- df[rep(df$Event, df$Years) ,]
df <- cbind(df, Count)
paste("The event number is", df$Event, "and the year number is", df$Count)
我认为 for 循环不是一个好主意,如果你可以矢量化的话。
我试图将数据框中的数值设置为 R 中 for 循环中的迭代器条件,但我无法让它工作。
脚本的思路是这样的:
- 遍历数据框中的行
- 对于每一行,根据行中的值指示循环遍历一个块
例如,我有一个名为 events
的数据框,如下所示:
Row Event Years
1 1 10
2 2 7
我想运行下面的循环来执行上面的脚本:
for (i in 1:nrow(events)){
for(j in 1:events[i,2]{
print(paste('The event number is',i,'and the year number is',j))
}
}
我正在寻找的结果如下所示:
[1] "The event number is 1 and the year number is 1."
[1] "The event number is 1 and the year number is 2."
[1] "The event number is 1 and the year number is 3."
[1] "The event number is 1 and the year number is 4."
[1] "The event number is 1 and the year number is 5."
[1] "The event number is 1 and the year number is 6."
[1] "The event number is 1 and the year number is 7."
[1] "The event number is 1 and the year number is 8."
[1] "The event number is 1 and the year number is 9."
[1] "The event number is 1 and the year number is 10."
[1] "The event number is 2 and the year number is 1."
[1] "The event number is 2 and the year number is 2."
[1] "The event number is 2 and the year number is 4."
[1] "The event number is 2 and the year number is 5."
[1] "The event number is 2 and the year number is 6."
[1] "The event number is 2 and the year number is 7."
但我得到的结果是:
[1] "The event number is 1 and the year number is 1."
[1] "The event number is 2 and the year number is 1."
[1] "The event number is 2 and the year number is 2."
出于测试目的,我尝试了以下代码,它按我希望的方式工作...
for (i in 1:nrow(events)){
for(j in 1:10){
print(paste('The event number is',i,'and the year number is',j))
}
}
...所以我尝试了各种方法来动态复制 10
值。
我是 R 新手,但我不是编程新手;我已经尝试了所有方法,从调用索引的方式到值的类型,再到使用 dim()
设置和获取维度,再到下面列出的其他方法:
1:as.numeric(events[i,2]
1:events$Years[i]
1:as.integer(events[i,2])
1:as.factor(events[i,2])
我认为我将迭代器视为单个值(如整数)而不是像因子或向量这样的 R 构造,这就是我遗漏的地方。我也读过有关使用 apply
函数解决此类问题的信息,但我不熟悉它的用法,所以我想先尝试解决 for
循环。
有谁知道我这里有什么问题吗?我一整天都在为这个烦恼!
在此先感谢您的帮助。
尝试:
df <- read.table(header = TRUE, text = "Row Event Years
1 1 10
2 2 7")
Count <- do.call("c", lapply(df$Years, function(x) seq(1, x)))
df <- df[rep(df$Event, df$Years) ,]
df <- cbind(df, Count)
paste("The event number is", df$Event, "and the year number is", df$Count)
我认为 for 循环不是一个好主意,如果你可以矢量化的话。