How do I import a csv file into variables?

I have written a script that does a number of calculations. This script needs 10 values for a calculation. Strings, Dates and numbers.

Alpha <- "Test"
Beta <- "B"
Gamma <- "C"
Delta <- "D"

Date_Low <- "03.02.2009"
Low <- 0.57100
Hoch <- 0.97900
Date_High <- "10.10.2010"

Grid <- 5
Parm <- 1000

In the csv file I have the headline in the first line and then 34 rows. Here's an example

"Alpha","Beta","Gamma","Delta","Date_Low","Low","High","Date_High","Grid","Parm"
"Test","B","C","D","09.08.2012","0.57100","0.97900","18.12.2008","5","1000"

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

Created on 2019-08-23 by the reprex package (v0.2.1)

I understand that you are trying to do something like the following code. Some of the changes I made were:

  1. I changed the data frame name to dat since data is a function in R. This was not necessary, just a preference I have.
  2. The assignment of Alpha and some other similar variables was wrong. I changed it to
Alpha <- dat[row, "Alpha"]
  1. A function was not needed to assign values to grid and index, so I simplified the code
  2. I included the assignments to grid and index, the creation of the data frame summarizing the row, and the writing of the csv within the for loop.
dat <- read.csv("TEST.csv", stringsAsFactors = FALSE)

for (row in 1:nrow(dat)) {
  Alpha <- dat[row, "Alpha"]
  Beta <- dat[row, "Beta"]
  Gamma <- dat[row, "Gamma"]
  Quote <- dat[row, "Delta"]
  Date_Low <- dat[row, "Date_low"]
  Tief <- dat[row, "Low"]
  Hoch <- dat[row, "High"]
  Date_High <- dat[row, "Date_High"]
  Grid <- dat[row, "Grid"]
  Einheiten <- dat[row, "Parm"]
  
  # Calculate the grid
  grid <- if (Quote == "JPY") (Grid / 100) else (Grid / 10000)
  
  # Calculate the Index
  index <- (Hoch - Tief)/grid + 1
  
  #I start a data.fram and bild the csv files
  tmp <- data.frame (
    Grid           = grid,
    Einheiten      = Einheiten,
    Index          = index
  )
  write.csv(tmp, file = paste0(Alpha, "_file.csv"), row.names = FALSE)
}

Alpha is assigned a value in the first line of code in the for loop.

for (row in 1:nrow(dat)) {
  Alpha <- dat[row, "Alpha"]

Is there a a typo in your version? If you cannot find the problem, please post your code

I think a for loop is not a good idea in this situation, this is how I would do it

library(tidyverse)

data <- read.csv("data.csv", header = TRUE, stringsAsFactors = FALSE)

data %>% 
    rename(Quote = "Delta", Tief = "Low", Hoch = "High", Einheiten = "Parm") %>%
    mutate(Grid = if_else(Quote == "JPY", Grid / 100, Grid / 10000),
           Index = (Hoch - Tief)/Grid + 1) %>%
    select(Grid, Einheiten, Index) %>% 
    group_nest(row_number()) %>% 
    .$data %>% 
    walk2(data$Alpha, ~write.csv(.x, file = paste0(.y, ".csv"), row.names = FALSE))

There are a couple of problems here:

  • 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.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.