R 中的嵌套 For 循环错误 "numerical expression has 2 elements: only the first used"
Nested For loop in R with error "numerical expression has 2 elements: only the first used"
我正在尝试使用 R 中的嵌套 for 循环打印字符串中 3 个连续字符的组合。该代码确实打印了这些组合,但是我收到一个错误,关于只对其中一个元素而不是每一行执行此操作数据框。
x <- data.frame(Pattern = c("abcdef", "hijklmnop"), id = 1:2)
output <- vector("character", length(x$Pattern))
for (i in 1:nrow(x)) {
file <- x$Pattern[i]
for (j in 1:(str_length(x$Pattern))) {
output[j] <- substr(file, j, j+2)
}
}
numerical expression has 2 elements: only the first usednumerical expression has 2 elements: only the first used
>
> output
[1] "hij" "ijk" "jkl" "klm" "lmn" "mno"
这里有 2 件事情不起作用。一个是启动的 var output
使用第一个模式的长度(长度 = 6)和基于该长度的打印组合,但是我正在寻找一个输出,它是字符串的长度(长度 = 9 ).预期输出如下,未使用嵌套 for 循环。
for (j in 1:9) {
output[j] <- substr(file, j, j+2)
}
output
[1] "hij" "ijk" "jkl" "klm" "lmn" "mno" "nop" "op" "p"
我 trim 进一步向下,以便每个字符串只有 3 个连续字符的组合列表。
list(output[1:(length(output)-3)])
[[1]]
[1] "hij" "ijk" "jkl" "klm" "lmn" "mno"
我遇到的第二个问题是输出仅打印列表中第二个字符串的组合。我已尝试按照其他帖子中的建议将 1:nrow(a)
更改为 seq_along
和 length(a)
,但这不起作用。预期输出如下。
a$combo <- output
a$combo
[1] c("abc","bcd","cde","def") c("hij","ijk","jkl","klm","lmn","mno")
x <- data.frame(Pattern = c("abcdef", "hijklmnop"), id = 1:2)
# number of additional letters in individual character string
add_letters = 2
library(stringr)
output = list()
for (i in 1:nrow(x)) {
file <- x$Pattern[i]
l = list()
for (j in 1:(str_length(x$Pattern[i])-add_letters)) {
l[j] <- c(substr(file, j, j+add_letters))
}
output[[i]] = l
}
x$combo = output
使用列表的解决方案 - 正如 Gregor Thomas 所建议的。
我正在尝试使用 R 中的嵌套 for 循环打印字符串中 3 个连续字符的组合。该代码确实打印了这些组合,但是我收到一个错误,关于只对其中一个元素而不是每一行执行此操作数据框。
x <- data.frame(Pattern = c("abcdef", "hijklmnop"), id = 1:2)
output <- vector("character", length(x$Pattern))
for (i in 1:nrow(x)) {
file <- x$Pattern[i]
for (j in 1:(str_length(x$Pattern))) {
output[j] <- substr(file, j, j+2)
}
}
numerical expression has 2 elements: only the first usednumerical expression has 2 elements: only the first used
>
> output
[1] "hij" "ijk" "jkl" "klm" "lmn" "mno"
这里有 2 件事情不起作用。一个是启动的 var output
使用第一个模式的长度(长度 = 6)和基于该长度的打印组合,但是我正在寻找一个输出,它是字符串的长度(长度 = 9 ).预期输出如下,未使用嵌套 for 循环。
for (j in 1:9) {
output[j] <- substr(file, j, j+2)
}
output
[1] "hij" "ijk" "jkl" "klm" "lmn" "mno" "nop" "op" "p"
我 trim 进一步向下,以便每个字符串只有 3 个连续字符的组合列表。
list(output[1:(length(output)-3)])
[[1]]
[1] "hij" "ijk" "jkl" "klm" "lmn" "mno"
我遇到的第二个问题是输出仅打印列表中第二个字符串的组合。我已尝试按照其他帖子中的建议将 1:nrow(a)
更改为 seq_along
和 length(a)
,但这不起作用。预期输出如下。
a$combo <- output
a$combo
[1] c("abc","bcd","cde","def") c("hij","ijk","jkl","klm","lmn","mno")
x <- data.frame(Pattern = c("abcdef", "hijklmnop"), id = 1:2)
# number of additional letters in individual character string
add_letters = 2
library(stringr)
output = list()
for (i in 1:nrow(x)) {
file <- x$Pattern[i]
l = list()
for (j in 1:(str_length(x$Pattern[i])-add_letters)) {
l[j] <- c(substr(file, j, j+add_letters))
}
output[[i]] = l
}
x$combo = output
使用列表的解决方案 - 正如 Gregor Thomas 所建议的。