Skip to content
Prev 383352 / 398502 Next

NA command in a 'for' loop

Thank you all for your input.

This is an example of one data file (I have 74 data files):

2.90546E+11, threat,    1, 2, 1, 2, 1,        death,        stove,           NA,           NA,  205,    0,  394
2.90546E+11, threat,    2, 2, 2, 1, 1,    emaciated,    shortened,           NA,           NA,  205,    0,  502
2.90546E+11, threat,    3, 1, 1, 1, 2,     mutilate,     consider,           NA,           NA,  205,    1,  468
2.90546E+11, threat,    6, 1, 2, 2, 1,         weep,         shop,           NA,           NA,  203,    1,  345
2.90546E+11, threat,    9, 2, 1, 2, 2,    tormented,    easygoing,           NA,           NA,  205,    1,  373
2.90546E+11, threat,   10, 1, 2, 2, 2,        snake,        table,           NA,           NA,  205,    1,  343
2.90546E+11, threat,   11, 2, 2, 1, 1,       crisis,       faucet,           NA,           NA,  203,    1,  437
2.90546E+11, threat,   12, 1, 1, 1, 1,       victim,      utensil,           NA,           NA,  203,    1,  343
2.90546E+11, threat,   14, 1, 2, 2, 1,    depressed,    repentant,           NA,           NA,  203,    1,  441
2.90546E+11, threat,   15, 2, 2, 1, 2,         scum,         shoe,           NA,           NA,  205,    1,  475

?Column 13 has values of 0s and 1s which my cognitive task outputted. Column 14 is the reaction time (ms) data. I want to get rid of the rows that contain zeros so I thought I'd first replace zeros with NAs then use complete.cases function to get rid of the NAs. I also wanted to apply other functions so I included them all in a loop. All work fine except for the one where I try to turn the zeros to NAs.

Jim when I tried your mockdata example, it worked fine. But when I translated it to my data, I still get zeros in the output. Can you identify any mistranslations I'm doing?

txt.files<-list.files(".",pattern="dotprobe") #all my data files are text files in one folder
for(tf in txt.files) {
  d<-read.table(tf)
  d[,13][d[,13]==0]<-NA #column 13 contains zeros
  d<-d[ ,-c(10,11)] #get rid of columns 10 and 11
  write.table(d,sub("[.]",".tlbs.",tf),quote=FALSE, row.names=FALSE)
}

That's an example of one of the output I get:

V1 V2 V3 V4 V5 V6 V7 V8 V9 V12 V13 V14
2.90546E+11, threat, 1, 2, 1, 2, 1, death, stove, 205, 0, 394
2.90546E+11, threat, 2, 2, 2, 1, 1, emaciated, shortened, 205, 0, 502
2.90546E+11, threat, 3, 1, 1, 1, 2, mutilate, consider, 205, 1, 468
2.90546E+11, threat, 6, 1, 2, 2, 1, weep, shop, 203, 1, 345
2.90546E+11, threat, 9, 2, 1, 2, 2, tormented, easygoing, 205, 1, 373
2.90546E+11, threat, 10, 1, 2, 2, 2, snake, table, 205, 1, 343

Columns 10 and 11 were deleted. But zeros were not replaced by NAs.
After all the data cleaning, the functions I'm interested in including in the loop are: get_tlbs and summarize_bias (and these also work fine in my loop).

Thanks again ?
Sincerely
Helen
Message-ID: <AM5PR10MB17468DE9CA0AAE5332C9DD0CB9D50@AM5PR10MB1746.EURPRD10.PROD.OUTLOOK.COM>
In-Reply-To: <CA+8X3fX8GYRrmHMf6p5z8HkNEF0ysicEOfU5_dT=owkhC-rCqA@mail.gmail.com>