In the same file path I have many different files which start with the same name. Example "myfile_"
.
The are csv
file.
I have a specific code which I would like to execute for every file.
The output of this code is 5 variables/columns.
Is there any way to read the data of every file insert them in the code and save the results in a dataframe which will have a column which will be the name of the file and the columns of the output of codes?
Reproducable example.
Let's say the following dataframe are the files:
employee <- c('John Doe','Peter Gynn','Jolie Hope')
salary <- c(21000, 23400, 26800)
startdate <- as.Date(c('2010-11-1','2008-3-25','2007-3-14'))
myfile_1 <- data.frame(employee, salary, startdate)
myfile_2 <- read.table(header=TRUE, text="text salary
laughter 8.50
happiness 8.44
love 8.42
happy 8.30
laughed 8.26
laugh 8.22")
An example of executing code:
sum <- sum(myfile_1$salary)
sub <- sum(myfile_1$salary)/2
addtwo <- sum(myfile_1$salary)+2
subtracttwo <- sum(myfile_1$salary)-2
doubleo <- sum(myfile_1$salary)*2
this are the command I would like to calculate for every file. So that's why I ask to load one file every time.
And as output have a df like this:
filename sum sub addtwo subtracttwo doubleo
myfile_1
myfile_2
and in the other columns the result of every execution