0

I have imported a CSV file to R but now I would like to extract a variable into a vector and analyse it separately. Could you please tell me how I could do that?

I know that the summary() function gives a rough idea but I would like to learn more.

I apologise if this is a trivial question but I have watched a number of tutorial videos and have not seen that anywhere.

Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
JohnK
  • 1,019
  • 3
  • 14
  • 30
  • 6
    You need to read [An Introduction to R](http://cran.r-project.org/doc/manuals/R-intro.pdf). – Roland Jun 11 '14 at 11:56

2 Answers2

2

Read data into data frame using read.csv. Get names of data frame. They should be the names of the CSV columns unless you've done something wrong. Use dollar-notation to get vectors by name. Try reading some tutorials instead of watching videos, then you can try stuff out.

d = read.csv("foo.csv")
names(d)
v = d$whatever # for example
hist(v) # for example

This is totally trivial stuff.

Spacedman
  • 92,590
  • 12
  • 140
  • 224
1

I assume you have use the read.csv() or the read.table() function to import your data in R. (You can have help directly in R with ? e.g. ?read.csv

So normally, you have a data.frame. And if you check the documentation the data.frame is described as a "[...]tightly coupled collections of variables which share many of the properties of matrices and of lists[...]"

So basically you can already handle your data as vector.

A quick research on SO gave back this two posts among others:

And I am sure they are more relevant ones. Try some good tutorials on R (videos are not so formative in this case). There is a ton of good ones on the Internet, e.g: * http://www.introductoryr.co.uk/R_Resources_for_Beginners.html (which lists some) or * http://tryr.codeschool.com/

Anyways, one way to deal with your csv would be:

#import the data to R as a data.frame
mydata = read.csv(file="SomeFile.csv", header = TRUE, sep = ",", 
quote = "\"",dec = ".", fill = TRUE, comment.char = "")

#extract a column to a vector
firstColumn = mydata$col1 # extract the column named "col1" of mydata to a vector
#This previous line is equivalent to:
firstColumn = mydata[,"col1"]

#extract a row to a vector
firstline = mydata[1,] #extract the first row of mydata to a vector

Edit: In some cases[1], you might need to coerce the data in a vector by applying functions such as as.numeric or as.character:

firstline=as.numeric(mydata[1,])#extract the first row of mydata to a vector
#Note: the entire row *has to be* numeric or compatible with that class

[1] e.g. it happened to me when I wanted to extract a row of a data.frame inside a nested function

Community
  • 1
  • 1
Mitra
  • 655
  • 8
  • 16