Great video, thanks for the help. What if I wanted to actually load in all the data files to the environment, how would I make the "data" in line 9 be a variable? thanks in advance
You wouldn't make the variable name `data` itself be a variable. There are typically two approaches to what you want to accomplish: 1) make `data` a list and store each data frame loaded by the loop as one position in that list, so the first time through the loop the data frame gets stored in the first position in the list, the second time through the loop it gets stored in the second position, and so on. You can see some of the basic ideas in our Looping By Index video: ruclips.net/video/vWj5rypEZ4U/видео.html The modified version of the code would look like: data
Thank you very much, but what if instead of getting a single vector called "results" I want to create a different data frame for each of the data files?
You're welcome! There's an answer to another question that lays out the two basic strategies when working with multiple data frames. Take a look and let me know if you have any more questions: ruclips.net/video/4-uWgh5kDSc/видео.html&lc=UgwqqFrWYTHi7aVTh7B4AaABAg.9bGLQ65trTb9bHwBtID_eI
@@larissacury7714 It should link to the comment, but it looks like it's doing something funny when clicked on from the comments section. It's the response to the comment M J 2 months ago. Here's a copy of it: There are typically two approaches to what you want to accomplish: 1) make `data` a list and store each data frame loaded by the loop as one position in that list, so the first time through the loop the data frame gets stored in the first position in the list, the second time through the loop it gets stored in the second position, and so on. You can see some of the basic ideas in our Looping By Index video: ruclips.net/video/vWj5rypEZ4U/видео.html The modified version of the code would look like: data
How can I use for loop to read in different file with with subscript in the file name. Example if I am reading in the first file, I want data1 and for second file, I want data2 and so on. Thanks for any help.
i have a data.frame with 17 column I want to replace each value by this formula new value=0.5+(0.5(x(each value in column)-mean(column)/(max(column)-min(column))
There are a few different ways to do this. To do it with a for loop you loop over the columns of the data frame using and index and then perform your calculation on each column storing it back into the original column: for (i in seq_along(df)) { df[i] = 0.5 + (0.5 * (df[[i]] - mean(df[[i]])/(max(df[[i]]) - min(df[[i]])))) } Our video on looping by index (ruclips.net/video/vWj5rypEZ4U/видео.html) will help explain some of the work with i. The one trick piece is the use of two sets of square brackets, which converts the column of the data frame into a vector so that mean, max, and min can work on it. The simplest way to do this is probably to use mutate_all() from dplyr: mutate_all(df, funs("normalized" = 0.5 + (0.5 * (.-mean(.)/(max(.)-min(.)))))) This will create a new column for each of your 17 columns with the word _normalized applied to the end applying the function to the data in the original column.
😂😂😂. The last bit cracks me up! "I'm retiring"
Glad you enjoyed it!
This video was amazing! Thank you so much for taking the time to explain everything in detail. I followed along and it worked smoothly.
Thanks! That’s awesome!
Thanks for explaining this! The video is great and it helped me a lot :)
You're welcome!
Great video, thanks for the help. What if I wanted to actually load in all the data files to the environment, how would I make the "data" in line 9 be a variable?
thanks in advance
You wouldn't make the variable name `data` itself be a variable. There are typically two approaches to what you want to accomplish:
1) make `data` a list and store each data frame loaded by the loop as one position in that list, so the first time through the loop the data frame gets stored in the first position in the list, the second time through the loop it gets stored in the second position, and so on. You can see some of the basic ideas in our Looping By Index video: ruclips.net/video/vWj5rypEZ4U/видео.html The modified version of the code would look like:
data
Thank you very much, but what if instead of getting a single vector called "results" I want to create a different data frame for each of the data files?
You're welcome! There's an answer to another question that lays out the two basic strategies when working with multiple data frames. Take a look and let me know if you have any more questions: ruclips.net/video/4-uWgh5kDSc/видео.html&lc=UgwqqFrWYTHi7aVTh7B4AaABAg.9bGLQ65trTb9bHwBtID_eI
@@weecology Thank you very much, but I guess that this link is the same of this video, isn't it?
@@larissacury7714 It should link to the comment, but it looks like it's doing something funny when clicked on from the comments section. It's the response to the comment M J 2 months ago. Here's a copy of it:
There are typically two approaches to what you want to accomplish:
1) make `data` a list and store each data frame loaded by the loop as one position in that list, so the first time through the loop the data frame gets stored in the first position in the list, the second time through the loop it gets stored in the second position, and so on. You can see some of the basic ideas in our Looping By Index video: ruclips.net/video/vWj5rypEZ4U/видео.html The modified version of the code would look like:
data
@@weecology thank you!! I'll take a look at it now!
How can I use for loop to read in different file with with subscript in the file name. Example if I am reading in the first file, I want data1 and for second file, I want data2 and so on. Thanks for any help.
You could do something like this:
for (i in 1:num_files){
filename
@@weecology Thanks.
oh men. its so short
i have a data.frame with 17 column I want to replace each value by this formula
new value=0.5+(0.5(x(each value in column)-mean(column)/(max(column)-min(column))
There are a few different ways to do this. To do it with a for loop you loop over the columns of the data frame using and index and then perform your calculation on each column storing it back into the original column:
for (i in seq_along(df)) {
df[i] = 0.5 + (0.5 * (df[[i]] - mean(df[[i]])/(max(df[[i]]) - min(df[[i]]))))
}
Our video on looping by index (ruclips.net/video/vWj5rypEZ4U/видео.html) will help explain some of the work with i. The one trick piece is the use of two sets of square brackets, which converts the column of the data frame into a vector so that mean, max, and min can work on it.
The simplest way to do this is probably to use mutate_all() from dplyr:
mutate_all(df, funs("normalized" = 0.5 + (0.5 * (.-mean(.)/(max(.)-min(.))))))
This will create a new column for each of your 17 columns with the word _normalized applied to the end applying the function to the data in the original column.
too much water