Dear R experts, I am using a 'for' loop to apply commands to multiple datasets (each file is one participant). The only one not working is the command that identifies zeros in my datasets and changes them to NAs. But when I look at the output, zeros ("0") are still present. Surprisingly, the functions work fine when I apply them to a single dataset (outside the loop). I've tried: all.files <- list.files(".") txt.files <- grep("threat.txt",all.files,value=T) for(i in txt.files){ d <- read.table(paste(i,sep=""),header=F) d[d==0] <- NA #replace zeros with NA write.table(d, paste0(i,".tlbs.txt"), quote=FALSE, row.names=TRUE)} d<-d[ ,-c(10,11)] d2<-d[complete.cases(d), ] d2$V4<-as.numeric(d2$V4) congruent <- (d2$V4 == 1) == TRUE x <- get_tlbs(d2$V14, congruent, prior_weights = NULL, method = "weighted", fill_gaps = FALSE) write.table(x, paste0(i,".tlbs.txt"), quote=FALSE, row.names=TRUE)} I've also tried: for(i in txt.files){ d <- read.table(paste(i,sep=""),header=F) if (0 %in% d) {replace_with_na(d,replace = list(x = 0))} # replace zeros with NA d<-d[ ,-c(10,11)] d2<-d[complete.cases(d), ] d2$V4<-as.numeric(d2$V4) congruent <- (d2$V4 == 1) == TRUE x <- get_tlbs(d2$V14, congruent, prior_weights = NULL, method = "weighted", fill_gaps = FALSE) write.table(x, paste0(i,".summaryoutput.txt"), quote=FALSE, row.names=TRUE)} Thank you for your help. Sincerely Helen [[alternative HTML version deleted]]
Hello, Instead of d[d == 0] <- NA try d[] <- lapply(d, function(x) {is.na(x) <- x == 0; x}) Also, in the first for loop paste(i, sep = "") does nothing, it's the same as i. And the same for (d2$V4 == 1) == TRUE Since (d2$V4 == 1) already is FALSE/TRUE there is no need for (.) == TRUE Hope this helps, Rui Barradas ?s 20:52 de 19/04/20, Helen Sawaya escreveu:> Dear R experts, > > I am using a 'for' loop to apply commands to multiple datasets (each file is one participant). The only one not working is the command that identifies zeros in my datasets and changes them to NAs. But when I look at the output, zeros ("0") are still present. Surprisingly, the functions work fine when I apply them to a single dataset (outside the loop). I've tried: > > all.files <- list.files(".") > txt.files <- grep("threat.txt",all.files,value=T) > > for(i in txt.files){ > d <- read.table(paste(i,sep=""),header=F) > d[d==0] <- NA #replace zeros with NA > write.table(d, paste0(i,".tlbs.txt"), quote=FALSE, row.names=TRUE)} > d<-d[ ,-c(10,11)] > d2<-d[complete.cases(d), ] > d2$V4<-as.numeric(d2$V4) > congruent <- (d2$V4 == 1) == TRUE > x <- get_tlbs(d2$V14, congruent, prior_weights = NULL, method = "weighted", fill_gaps = FALSE) > write.table(x, paste0(i,".tlbs.txt"), quote=FALSE, row.names=TRUE)} > > I've also tried: > > for(i in txt.files){ > d <- read.table(paste(i,sep=""),header=F) > if (0 %in% d) > {replace_with_na(d,replace = list(x = 0))} # replace zeros with NA > d<-d[ ,-c(10,11)] > d2<-d[complete.cases(d), ] > d2$V4<-as.numeric(d2$V4) > congruent <- (d2$V4 == 1) == TRUE > x <- get_tlbs(d2$V14, congruent, prior_weights = NULL, method = "weighted", fill_gaps = FALSE) > write.table(x, paste0(i,".summaryoutput.txt"), quote=FALSE, row.names=TRUE)} > > Thank you for your help. > Sincerely > Helen > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Thank you for your reply. I tried d[] <- lapply(d, function(x) {is.na(x) <- x == 0; x}) ?but I am still getting zeros instead of NAs in my output.. I wonder if the problem is that some of my data files don't have any zeros (participants made no errors).. ________________________________ From: Rui Barradas <ruipbarradas at sapo.pt> Sent: Monday, April 20, 2020 9:05 AM To: Helen Sawaya <helensawaya at hotmail.com>; r-help at R-project.org <r-help at R-project.org> Subject: Re: [R] NA command in a 'for' loop Hello, Instead of d[d == 0] <- NA try d[] <- lapply(d, function(x) {is.na(x) <- x == 0; x}) Also, in the first for loop paste(i, sep = "") does nothing, it's the same as i. And the same for (d2$V4 == 1) == TRUE Since (d2$V4 == 1) already is FALSE/TRUE there is no need for (.) == TRUE Hope this helps, Rui Barradas ?s 20:52 de 19/04/20, Helen Sawaya escreveu:> Dear R experts, > > I am using a 'for' loop to apply commands to multiple datasets (each file is one participant). The only one not working is the command that identifies zeros in my datasets and changes them to NAs. But when I look at the output, zeros ("0") are still present. Surprisingly, the functions work fine when I apply them to a single dataset (outside the loop). I've tried: > > all.files <- list.files(".") > txt.files <- grep("threat.txt",all.files,value=T) > > for(i in txt.files){ > d <- read.table(paste(i,sep=""),header=F) > d[d==0] <- NA #replace zeros with NA > write.table(d, paste0(i,".tlbs.txt"), quote=FALSE, row.names=TRUE)} > d<-d[ ,-c(10,11)] > d2<-d[complete.cases(d), ] > d2$V4<-as.numeric(d2$V4) > congruent <- (d2$V4 == 1) == TRUE > x <- get_tlbs(d2$V14, congruent, prior_weights = NULL, method = "weighted", fill_gaps = FALSE) > write.table(x, paste0(i,".tlbs.txt"), quote=FALSE, row.names=TRUE)} > > I've also tried: > > for(i in txt.files){ > d <- read.table(paste(i,sep=""),header=F) > if (0 %in% d) > {replace_with_na(d,replace = list(x = 0))} # replace zeros with NA > d<-d[ ,-c(10,11)] > d2<-d[complete.cases(d), ] > d2$V4<-as.numeric(d2$V4) > congruent <- (d2$V4 == 1) == TRUE > x <- get_tlbs(d2$V14, congruent, prior_weights = NULL, method = "weighted", fill_gaps = FALSE) > write.table(x, paste0(i,".summaryoutput.txt"), quote=FALSE, row.names=TRUE)} > > Thank you for your help. > Sincerely > Helen > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]