How do I apply the same functions to multiple files in R - r

Hi I am quite new to R programming. What I want to do is to replicate a series of actions to multiple files. My first step is to create a function that reads a file, and then performs subsequent actions.
For example
analyze <- function(filename){data<- read.csv(filename, header=TRUE)
average<- mean(data[,2])
print (average)}
analyze ("my first file")
However, I am having a problem with the code, because it does not give the right result. data is not updated when I change the filename. I don't know what went wrong. Can anyone give me a simpler alternative solution? Many thanks.

Related

How to create a loop with R function specgram(signal)

I am working with many signals; each one in a time series but is too many and, I need to make more than 1000 but, I am not sure how to implement it because I not only need the plots but the values of output for each spectrogram stored in a file or an R object. I am sorry I don't have an approach. Can anyone help out, please?

R- Not Loading all 5 variables only 1

Last night I attended my 1st R class and I am having some difficulties using the read.csv function. When I tried to run the function it is only uploading the 1st variable.
Does anyone know why this is happening. I don't know if it makes a difference but I am using a Mac.
I don't know if this helps, but sometimes when I create a .csv file from an excel file all of the variables collapse into one variable (I don't know why). Check the structure of your dataset using head(df) or str(df) (where df is the name of your data) and confirm that this didn't happen. If it did happen, you'll have to fix the .csv. If that isn't the problem, I'm not sure what the issue is.
For future reference it would help for you to supply a reproducible sample of your data as well as the full script that you are using.

R simmer package question about get_mon_arrivals

I'm currently learning how to use the simmer package in R in order to simulate processes.
I'm trying to gather information regarding a simulation I've built, using the get_mon_arrivals function.
I've noticed something weird about running this function - when I run:
arrivalData <- get_mon_arrivals(Mall)
arrivalDataOngoing <- get_mon_arrivals(Mall,ongoing=TRUE)
I get 2 different tables - just as expected, the first one containing rows for finished customers only, while the second one contains rows for unfinished customers as well, which are the customers that were generated but the simulation ended before they managed to finish the trajectory.
But if I write it the other way around, meaning:
arrivalDataOngoing <- get_mon_arrivals(Mall,ongoing=TRUE)
arrivalData <- get_mon_arrivals(Mall)
I get the same exact table in both cases. I know it's not something important, but I would really like to understand WHY it does that. I know I can fix it easily by going with the first option, but I am a man who likes to understand what he does.
Thanks alot for the help

How to continue project in new file in R

I have a large population survey dataset for a project and the first step is to make exclusions and have a final dataset for analyses. To organize my work, I must continue my work in a new file where I derive survey variables correctly. Is there a command used to continue work by saving all the previous data and code to the new file?
I don´t think I understand the problem you have. You can always create multiple .R files and split the code among them as you wish, and you can also arrange those files as you see fit in the file system (group them in the same folder with informative names and comments, etc...).
As for the data side of the problem, you can load your data into R, make any changes / filters needed, and then save it to another file with one of the billions of functions to write stuff to the disk: write.table() from base, fwrite() from data.table (which can be MUCH faster), etc...
I feel that my answer is way too obvious. When you say "project" you mean "something I have to get done" or the actual projects that you can create in rstudio. If it´s the first, then I think I have covered it. If it´s the second, I never got to use that feature so I am not going to be able to help :(
Maybe you can elaborate a bit more.

R loop from a txt or csv file

I am learning to use R and I am working with the for loop
Here is an example:
for (loopvalues in c(1,5,8,10,19)){
print(paste("The number is", loopvalues))
}
I was wondering what can be done if the list of values is as big as 100 or 1000 different values and they follow no patterns.
I imagined I can have the values saved in a csv or a txt file beforehand, but how could I tell the loop command to read the values from that file?
I am sure the question is very basic, so I thank you beforehand for your help!
For loops can be used for extremely long lists however you will often find that they become slow and you will want to use other commands such as the apply family.
You do not have to name all the values in the loop. One way to accomplish this is using the in. Here is an example using the mtcars data set that is preloaded into R.
for(c in unique(mtcars$carb)){
print(c)
}
By using the unique function, I don't even have to know what all the possible values of mtcars$carb are but I can still loop through them.
Additionally, you probably want to practice your googling skills instead of asking StackOverflow. Most of the questions you're going to ask when learning R are already out there.

Resources