在 R strsplit() 函数中:添加索引值有什么区别?

in R's strplit() function: What difference does adding an index value make?

我正在学习 R,我从一个练习中获得了以下代码,该练习的目标是编写代码来计算 rquote 中第一个 u 之前出现的 r 的数量。

rquote <- "r's internals are irrefutably intriguing"
chars <- strsplit(rquote, split = "")[[1]]

# Initialize rcount
rcount <- 0

# Finish the for loop
for (char in chars) {
  if (char == "r"){
      rcount = rcount + 1
  }
  if (char == "u"){
      break
  }
  
}

# Print out rcount
print(rcount)

我的问题是:在 strsplit() 末尾添加 [[1]] 有什么区别?为什么在排除 [[1]] 时遇到第一个“r”后 for 循环停止?

我知道 [[1]]for 循环按预期工作所必需的;但是,无论是否添加 [[1]]chars 都是相同的,即:

chars <- strsplit(rquote, split = "")[[1]] & chars <- strsplit(rquote, split = "") 两者都产生:

>chars
 [1] "r" "'" "s" " " "i" "n" "t" "e" "r" "n" "a" "l" "s" " " "a" "r" "e" " " "i"
[20] "r" "r" "e" "f" "u" "t" "a" "b" "l" "y" " " "i" "n" "t" "r" "i" "g" "u" "i"
[39] "n" "g"

chars用不用[[1]]是不一样的

当您使用 [[1]] 时:

chars1 <- strsplit(rquote, split = "")[[1]]
chars1
# [1] "r" "'" "s" " " "i" "n" "t" "e" "r" "n" "a" "l" "s" " " "a" "r" "e" " " "i" "r" "r" "e"
#[23] "f" "u" "t" "a" "b" "l" "y" " " "i" "n" "t" "r" "i" "g" "u" "i" "n" "g"

length(chars1)
#[1] 40

class(chars1)
#[1] "character"

不使用时 [[1]] :

chars2 <- strsplit(rquote, split = "")
chars2
#[[1]] <- Don't forget this [[1]], it is important
# [1] "r" "'" "s" " " "i" "n" "t" "e" "r" "n" "a" "l" "s" " " "a" "r" "e" " " "i" "r" "r" "e"
#[23] "f" "u" "t" "a" "b" "l" "y" " " "i" "n" "t" "r" "i" "g" "u" "i" "n" "g"

length(chars2)
#[1] 1

class(chars2)
#[1] "list"

strsplit returns 一个列表,所以添加 [[1]] 使列表成为字符向量。当你遍历任何对象时,你遍历它的长度,在 chars1 的情况下是 40,在 chars2 的情况下是 1.