Home > front end >  Reading CSV file in R, separate based on cell values
Reading CSV file in R, separate based on cell values

Time:03-29

I am having trouble reading a csv file into R and separating one large csv file into multiple datasets in R based on the value in the first row of the cell

I have a csv dataset that looks like:

Type Year Rate
a    0     x
a    1     x
a    2     x
b    0     x
b    1     x
c    0     x
c    1     x
c    2     x
c    3     x

Is there a way to read this into r and have it go into multiple different datasets based on the unique type?

It's a csv with annual data on a bunch of different technologies and I want to measure each individually. The quantity of entries for each type are unique.

I'm new to R so i'm not sure on the exact syntax but something like:

dataSetA \<- read.csv("mydata.csv", where type = "a")
dataSetB \<- read.csv("mydata.csv", where type = "b")

CodePudding user response:

You can also read in the .csv first, then split the dataframe into a list of dataframes by Type:

split(iris, iris$Species)
# Or with your data.
# split(df, df$Type

Or if you want to save them as separate dataframes to your global environment, then you can use list2env.

list2env( split(iris, iris$Species) , .GlobalEnv )

CodePudding user response:

Since your data fit completely in memory, you can simply use read.csv and dplyr::filter which has a syntax intuitively close to the logical statement you inserted into read.csv

Taking as example the iris dataset:

library(dplyr)
iris %>% filter(Species == "setosa") %>% head()

  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa
iris %>% filter(Species == "virginica") %>% head()
  Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
1          6.3         3.3          6.0         2.5 virginica
2          5.8         2.7          5.1         1.9 virginica
3          7.1         3.0          5.9         2.1 virginica
4          6.3         2.9          5.6         1.8 virginica
5          6.5         3.0          5.8         2.2 virginica
6          7.6         3.0          6.6         2.1 virginica

and so on and so forth.

  • Related