r - Unwanted rows in data.frame -
i want write function checks number of files name id.csv , return number of rows no na
s. here wrote:
complete <- function(directory, id) { setwd(directory) my_list <- list.files(getwd(), pattern="*.csv", full.names=true) my_id <- numeric() my_output<-data.frame() my_count<-numeric() for(integer in id){ my_data <- read.csv(my_list[integer]) my_subset <- subset(my_data, sulfate !=0 & nitrate !=0) my_count[integer]<-length(my_subset[[1]]) my_id[integer] <- integer } my_output<-cbind(my_id, my_count) my_output } complete("/home/jpasquier/téléchargements/specdata", c(1,3))
but here result:
> complete("/home/jpasquier/téléchargements/specdata", c(1,3)) my_id my_count [1,] 1 117 [2,] na na [3,] 3 243
so don't understand why there unwanted row number2, after there no integer equal 2 in id argument. thanks.
you getting na
s because assigning values 1st , 3rd value of variable, , nothing 2nd.
example:
v <- vector() v #logical(0) v[1] <- 111 v[3] <- 333 v #[1] 111 na 333
edit:
here working example, no need create function:
#create dummy csv df1 <- data.frame(x=c(1,na,2,0,4, 4), y=c(1,3, 4,0,na,6)) df2 <- data.frame(x=c(1,0,2,0, 4,0), y=c(1,0,4,0,na,0)) write.csv(df1,"id_1.csv",row.names = false) write.csv(df1,"id_2.csv",row.names = false) write.csv(df2,"id_3.csv",row.names = false) write.csv(df2,"id_4.csv",row.names = false) #choose csv id <- c(1,3) #get non 0 counts res <- lapply(lapply(list.files(pattern="^id.*.csv")[id],read.csv), function(i)nrow(subset(i,x!=0,y!=0))) #update result names names(res) <- list.files(pattern="^id.*.csv")[id] #output result counts res # $id_1.csv # [1] 4 # # $id_3.csv # [1] 3
Comments
Post a Comment