I want to import multiple data in R and find the average of the third column of each files. I have shown example below.
I have imported multiple files in R using Ramnath's solution from Import multiple text files in R and assign them names from a predetermined list . The code I have used so far is as follows:
#Import mulitple text using following code: files with extension *.dat
txt_files =list.files(pattern='\\.dat$')
data_list=lapply(txt_files,read.table,sep="\t",header=T)
Used Nico's answer to change to data frame from R list to data frame
# Change the list to dataframe
hello <- as.data.frame(do.call(rbind,data_list))
dim(hello)
# Using 12 files I got the following information
> dim(hello)
[1] 58536 1
Each file has 4878 number of rows. This is not what I am looking for. What above code did is merged all the data into one data frame based on rows.
I want it by columns and be able to calculate the average of third column from each file. I want to use the third column of each file and the find the array of average.
The sample of what I want is as follows:
File 1
Lat Long Value
10 12 15
12 13 16
File 2
Lat Long Value
10 12 11
12 13 15
Final File
Lat Long Value
10 12 13
12 13 15.5
As you can see for the final file, the first two columns are same, only thing that is different is the third column which is the average of two values from two files. So, I want to use my data to change to the data frame similar to the final file as shown above.