Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import data to a vector in R

Tags:

import

r

csv

vector

I would like to import CSV data into R in the form of an object of one dimension such as a vector. I only manage to import my data in the form of a table. I tried to convert the table into a vector, however it is not only challenging to my lack of expertise in R but also seems to lack parsimony for such a basic function.

Is there a straightforward way to accomplish this basic task, similar to the way c(x,y,z,...) works?

My data looks like the following (with 24,000 values): 1417656631000,0,0,3,20450,2,7,30798,2,2,7449,3,5,16002,2,1,77666,2,8,7435,4

like image 833
MartinGalilee Avatar asked Dec 15 '14 05:12

MartinGalilee


People also ask

How do I import data into a variable in R?

We can import the data into R using the read_csv() function; this is part of the readr package, which is part of the tidyverse . Let's make a new script for this episode, by choosing the menu options File, New File, R Script. We see that the read_csv() table reports a “column specification”.

Can you import data into R?

Importing data into R is fairly simple. For Stata and Systat, use the foreign package. For SPSS and SAS I would recommend the Hmisc package for ease and functionality. See the Quick-R section on packages, for information on obtaining and installing the these packages.

How do I load data into an R package?

If you look at the package listing in the Packages panel, you will find a package called datasets. Simply check the checkbox next to the package name to load the package and gain access to the datasets. You can also click on the package name and RStudio will open a help file describing the datasets in this package.


1 Answers

You could use scan

 op <- options(scipen=999)
 res <- scan('yourfile.csv', what=numeric(), sep=",", quiet=TRUE)
 res
 #[1] 1417656631000             0             0             3         20450
 #[6]             2             7         30798             2             2
 #[11]          7449             3             5         16002             2
 #[16]             1         77666             2             8          7435
 #[21]             4
like image 157
akrun Avatar answered Oct 19 '22 23:10

akrun