嵌套for循环无法循环

Nested for loop fails to loop

我编写了以下嵌套 for 循环,它应该遍历数据框 df,并在列 a 中创建 1,如果行 q 中有 a。通常,对于列 a、b 和 c,如果列 q 中的条目与列名称匹配,则列 a、b 和 c 应通过具有一个来表示。

由于我无法很好地描述它,这里有一段代码来说明我的意思。

此时,生成的 df 在相应的第三行的 c 列中只有 1,但在 a 或 b 列中没有。

    df = data.frame(q=c("a","b","c"),a=c(0,0,0),b=c(0,0,0),c=c(0,0,0))

    for (x in nrow(df)) {
  for (y in ncol(df)) { 
if (colnames(df[y]) == df$q[x]) { 
  df[x,y] = 1} 
}}

A picture of the intended output (whereas the red "1s" do not appear at this moment:

此外,我正在处理的实际数据框大约有 100 000 行和 100 列。

谢谢!

因为 for (x in nrow(df)) 意味着 x = 3, cause nrow(df) returns 3. 你应该这样写: 对于 (x in 1 : nrow(df)),其中 return 1 2 3.

for (x in 1 : nrow(df)) {
  for (y in 1 : ncol(df)) { 
    if (colnames(df[y]) == df$q[x]) { 
      df[x,y] = 1} 
  }
}

现在 df 就是你想要的。

这是避免循环的另一种选择。

library(tidyr)       
library(dplyr)
gather(df, key, value, -q) %>%
 mutate(value = if_else(q == key, 1, 0)) %>%
 spread(key, value)
 #  q a b c
 #1 a 1 0 0
 #2 b 0 1 0
 #3 c 0 0 1

gatherabc,然后将新创建的列 keyq 进行比较。如果各自的值相同,赋值1,否则0.