I want to import the csv. data <- read.csv("data.csv", header = TRUE)
How do I read the csv file correctly and is a foreach loop the right choice? How do I pass the values to the function? Is it possible to help with some lines of code?
This is the sort of thing you can do with the mutate function of the dplyr package, though there are other ways also. It will calculate a value for each row of the data frame you make by reading in the csv file.
library(dplyr)
df <- data.frame(A = 1:3, B = 2:4, C = 3:5, D = 4:6)
df
#> A B C D
#> 1 1 2 3 4
#> 2 2 3 4 5
#> 3 3 4 5 6
#Compute within mutate()
df <- df %>% mutate(NewVal = A^2 * B + C - D)
df
#> A B C D NewVal
#> 1 1 2 3 4 1
#> 2 2 3 4 5 11
#> 3 3 4 5 6 35
#Or write a function to do the calculation
df <- data.frame(A = 1:3, B = 2:4, C = 3:5, D = 4:6)
MyFunc <- function(x) {
x$A^2 * x$B + x$C - x$D
}
df <- df %>% mutate(NewVal = MyFunc(.))
df
#> A B C D NewVal
#> 1 1 2 3 4 1
#> 2 2 3 4 5 11
#> 3 3 4 5 6 35
You shouldn't be using the assignment operator <- inside a mutate() statement, use = instead.
kurs = inside mutate() is expecting a single value not a vector, as produced by seq(). Could you explain what are you trying to accomplish with that code? a sample of your desired output would be useful.
The script is not supposed to show any output in the console, it just silently creates the requested .csv files in the working directory, have you looked into the folder?
Sorry but I can't understand the logic behind this, it seems like you are trying to generate sequences of different length for each row in your dataframe an some how you expect to merge them together into a single dataframe.
In your original sample data (in the post you have deleted) you had a column named Alpha containing values like "test", "test1", "test2" wich matched the names for the .csv files as you had requested, but in this new sample data, there is no Alpha column so the walk2() function has nothing to iterate with and generates no files.