How to create lope to use same code for analyzing data from multiple files and store result in one variable ?

Dear All,

I have created code to analyse the data of one file and I have three results from this analysis of data.

I want to now create lope to use same code to analyse data from multiple files and store the results of multiple files in one variable. I would like to differentiate results of each file based on file name.

Could someone help me with this.

My present code -

> library(stringr)
> library(readr)
> myFile = readLines(file.choose())
> myResult = list() 
> #Find the position of the variables that are between [] 
> #We add the last line number as a position as well
> vars = c(which(str_detect(myFile, "^\\[.*\\]\\s*$") == T), length(myFile))
> #get the content for each variable 
> for(i in 1:(length(vars)-1)){
+   myData = myFile[vars[i]:(vars[i+1]-1)]
+ #remove lines that are comments or blank
+ #if content is a list of variables, create them as a list
+ if(str_detect(myData[2],"=")){
+   content = str_split(myData[-1],"=")
+   result = lapply(lapply(content,"[",2), parse_guess)
+   names(result) = sapply(content,"[",1)
+ } else{
+   #if the content just a vector of data, extract it
+   result = parse_guess(myData[-1])
+ }
+ #create the variable as a list item and assign the content 
+ myResult[[str_remove_all(myData[1], "\\[\\]")]]=result
+ }
> myFile = myResult$`[specdata0]`
> n=round((myResult$`[specchannel0]`$fRPMmean*4/myResult$`[specchannel0]`$dF)) 
#number of data points to be considered from complete set
> myFile=myFile[1:n]  #required number of data for analysis 
> myFile = as.numeric(myFile)  
> l=round(n/4)  # number of data per group
> #defining each group 
> myFile1=myFile[1:l] 
> myFile2=myFile[(l+1):(2*l)]
> myFile3=myFile[(2*l+1):(3*l)]
> myFile4=myFile[(3*l+1):n]
> #all predictor calulations 
> mean1 = mean(myFile1)
> mean2 = mean(myFile2)
> mean3 = mean(myFile3)
> mean4 = mean(myFile4)
> Mean <- data.frame(mean1,mean2,mean3,mean4)
> Var1 = var(myFile1)
> Var2 = var(myFile2)
> Var3 = var(myFile3)
> Var4 = var(myFile4)
> Var <- data.frame(Var1,Var2,Var3,Var4)
> StandDiv1 = sd(myFile1)
> StandDiv2 = sd(myFile2)
> StandDiv3 = sd(myFile3)
> StandDiv4 = sd(myFile4)
> StandDiv <- data.frame(StandDiv1,StandDiv2,StandDiv3,StandDiv4)
> R <- data.frame(Mean,Var,StandDiv) #This has all results 

Thank You

This code is for one file and so far I saved results in variable called "R" but I would like to store results using name of file and want to continue same for rest files.

Hi, welcome!

Could you turn this into a minimal REPRoducible EXample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help.

If you've never heard of a reprex before, you might want to start by reading this FAQ:

You mean I should create example code using standard data set from R.

I mean that you should provide a proper reproducible example, including sample data of some sort that allow us to run your code and reproduce the problem.
Please read the guide for more details about this.

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