Nested for loop fails to loop

时间:2018-02-03 11:20:56

标签: r for-loop dataframe nested nested-loops

I have written the following nested for loop which is supposed to iterate over the data frame, df, and create 1 in the column a, if the row q has a in it. Generally, for columns a,b and c, if the entry in column q matches the column name, columns a,b and c should indicate that by having one.

As I am not able to describe it well, here is a piece of code to illustrate what I mean.

At this moment, the resulting df only has an 1 in column c,in the respective third row, but not for column a or 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:

Moreover, the actual dataframe I am working on is roughly 100 000 rows and 100 columns.

Thanks!

2 个答案:

答案 0 :(得分:2)

因为(x in nrow(df))表示x = 3,因此nrow(df)返回3.你应该这样写: for(x in 1:nrow(df)),返回1 2 3。

<class 'NoneType'>

现在df就是你想要的。

答案 1 :(得分:1)

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

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