How to read in column vectors from a .csv file in R

36,787

First, vectors are simply a sequence of data elements. And data frames are lists of equal length vectors.

Hence, you can easily reference each column of a data frame as a vector.

df <- read.csv('C:\\Path\\To\\DataFile.csv')

v1 <- df[[1]]  # by column number
v2 <- df[["col1"]]  # by column name
v3 <- df$col1  # by column name
Share:
36,787
Zeke
Author by

Zeke

Updated on March 12, 2020

Comments

  • Zeke
    Zeke about 4 years

    So I have data being processed in Python that I'm outputting as a .csv file. I would like R to be able to read the .csv file in such a way that it turns it into a dataframe in which some of the columns are actually vectors.

    Is this even possible and how would I format a .csv so that this could happen? Thanks!