Using multiple different .csv files in a loop to inject another r file

I am still new with RStudio, i will appreciate if someone give me an idea

setwd("~/lastversion_gwas_analysis/F_95/trial/") 
source("gwas.r") 
source("emma.r")
source("plots_gwas.r")
source('~/lastversion_gwas_analysis/f95_gwas.r')
#load Y values
temp = list.files(pattern="*.csv")
for (i in 1:length(temp)) assign(temp[i], read.csv(temp[i]))
#i can just put one .csv file as a Y value inside f95_gwas() r file.
Y<- AT5G66980_AT4G10370.csv
Y[,1]<-as.numeric(Y[,1])
Y[,2]<-as.numeric(Y[,2])
f95_gwas(Y) #in this command gwas is running'''
#in here i tried to to put into list then call them separetly but i didnt find any solution yet. i need to use this for thousand differnt .csv file and put separelty inside f95_gwas () as a Y value
library(tidyverse)
library(fs)
source("gwas.r") 
source("emma.r")
source("plots_gwas.r")
source('~/lastversion_gwas_analysis/f95_gwas.r')
setwd("~/lastversion_gwas_analysis/F_95/trial/") 
file_paths<-fs::dir_ls("~/lastversion_gwas_analysis/F_95/trial/")
file_paths
##for loop
Y <- list()
for(i in seq_along(file_paths)){
  Y[[i]]<-read_csv(
    file=file_paths[[i]]
  )
}
Y<-set_names(Y, file_paths)
for (i in 1:length(Y))
{
  f95_gwas(Y[i])  #in this command gwas is running  
}

I wasnt sure to put into loop like this, it was just an idea but it give me an Error in Y[,1]: incorrect number of dimensions. So i need to use these different .csv files as a separate Y value into the f95_gwas funtion.

My first thought is to use double brackets to index Y

for (i in 1:length(Y))
{
  f95_gwas(Y[[i]])  #in this command gwas is running  
}

Thank you for your answer, i also tried with double brackets but it still give me an error:
Error in Y[,1]: incorrect number of dimensions.
So i think i need to find a different approach to put different files into loop as a seperate Y value into the funtion f95_gwas().

What is the result of

class(Y[[1]])

I got this output:

[1] "spec_tbl_df" "tbl_df" "tbl" "data.frame"

So you do have a data frame stored in Y[[1]]. If you run this much of your code

file_paths<-fs::dir_ls("~/lastversion_gwas_analysis/F_95/trial/")
file_paths
##for loop
Y <- list()
for(i in seq_along(file_paths)){
  Y[[i]]<-read_csv(
    file=file_paths[[i]]
  )
}
Y<-set_names(Y, file_paths)

and then run

f95_gwas(Y[[1]])

do you get the error? If so, run

str(Y[[1]])

and see if the data frame has the structure you expect.

I think i shouldn't try to put f95_gwas(Y[[i]]). Instead of this i need to put directlt f95_gwas(Y) so maybe i shouldnt create an list in the first place to put all these file in the list.

In the first code you posted, when you are processing a single file, you have the lines

Y[,1]<-as.numeric(Y[,1])
Y[,2]<-as.numeric(Y[,2])
f95_gwas(Y)

I do not see similar lines processing the first two columns with as.numeric() when you are using the loop. That may be the cause of the error.

It is certainly possible to combine your two loops so that you use read_csv() to assign the content of a file to Y and then process Y with f95_gwas() (including using as.numeric() to change the first two columns).

I notice that you do not assign the result of f95_gwas() to anything. That might be alright, depending on what the function does, but I mention it because it is a common error to forget to assign the function's returned value.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.