Save Imported CSV Data in Vector - R

Save imported csv data in vector - R

With the code I provided a simple solution could be to extract simply the row, but it looks too much easy maybe I missed something.

new_dat <- dat[1, ]
new_dat
[1] 1 4 7 10 13 16 19 22 25

Edit

My solution works well but it is not efficient. Here I have an improved loop versions so you can store objects separately in only one command.

First define elements that will be the name of the objects:

val <- c(1:3)
nam <- "new_dat_"

and then extract all elements with the loop.

for(i in 1:nrow(dat)){ assign(paste(nam, val, sep = "")[i], dat[i, ]) }

after that use ls() and you should see 3 elements named new_dat_1","new_dat_2", "new_dat_3" "val" each of them contains one row of your dat. This solution can be very helpful if you have to extract several rows and not just one and lead to this output:

new_dat_3
[1] 3 6 9 12 15 18 21 24 27
new_dat_1
[1] 1 4 7 10 13 16 19 22 25
new_dat_2
[1] 2 5 8 11 14 17 20 23 26

Read csv file as a vector

We can use scan

scan("filetoread.csv", sep=',', what = "", quiet = TRUE)
#[1] "0610010K14Rik" "0610011F06Rik" "1110032F04Rik" "1110034G24Rik"
#[5] "1500011B03Rik" "1700019L03Rik" "1700021K19Rik" " blah" "blah"

How to Export/Import Vectors in R?

There are a number of ways to read and write data/files in R. For reading, you may want to look at: read.table, read.csv, readLines, source, dget, load, unserialize, and readRDS. For writing, you will want to look write.table, writeLines, dump, dput, save, serialize, and saveRDS.

x <- 1:3
# [1] 1 2 3
save(x, file = "myvector.rda")

# Change x to prove a point.
x <- 4:6
x
# [1] 4 5 6

# Better yet, we could remove it entirely
rm(x)
x
# Error: object 'x' not found

# Now load what we saved to get us back to where we started.
load("myvector.rda")
x
# [1] 1 2 3

Alternatively, you can use saveRDS and readRDS -- best practice/convention is to use the .rds extension; note, however, that loading the object is slightly different as saveRDS does not save the object name:

saveRDS(x, file = "myvector_serialized.rds")
x <- readRDS("myvector_serialized.rds")

Finally, saveRDS is a lower-level function and therefore can only save one object a time. The traditional save approach allows you to save multiple objects at the same time, but can become a nightmare if you re-use the same names in different projects/files/scripts...

How to import data to a vector in R

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

How to convert a csv list to a character vector in R

Given a file that only has one column file_1.csv, e.g.:

F653
F763
F121
F123

... it isn't really a CSV file because there are no commas separating the values. Nevertheless, you can read it with dat1 <- read.csv("file_1.csv",header=F) to obtain:

> dat1
V1
1 F653
2 F763
3 F121
4 F123

Alternatively, a two-column comma-separated file file.csv:

F653,1
F763,2
F121,3
F123,4

... the file can be read in like:

> dat <- read.csv("file.csv",header=F)
> dat
V1 V2
1 F653 1
2 F763 2
3 F121 3
4 F123 4

However, dat and dat1 are both data tables. If you want a vector instead of a data table from file_1.csv, you can get that like this:

> dat <- read.csv("file.csv",header=F)$V1
> dat
[1] F653 F763 F121 F123
Levels: F121 F123 F653 F763

You can see that the vector has been read as a factor by default.

If you want a character vector, you can get that from:

> as.character(dat)
[1] "F653" "F763" "F121" "F123"

Read csv into a vector with comma separated elements

I'm not sure I understand what you're after.

You can use readLines to read the file line-by-line; if necessary, concatenate all entries with paste0:

data <- readLines("data.csv");
long_vector_of_entries <- paste0(data, collapse = ",");

Not sure how useful this format would be though.


Update

If you just want all entries in a single vector, you can simply do:

data <- read.csv("data.csv", header = TRUE, row.names = 1)
as.numeric(t(df));


Related Topics



Leave a reply



Submit