Save imported csv data in vector - R -
i have found read.csv("file.csv")$v1
may split exported table columns data organised in row-by-row fashion, record elements vector sequentially element[1][1] ->...->element[n][n]
. thoughts how accomplished in r?
update: once imported mydata looks like:
dat <- matrix(1:27, nrow = 3) dat [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [1,] 1 4 7 10 13 16 19 22 25 [2,] 2 5 8 11 14 17 20 23 26 [3,] 3 6 9 12 15 18 21 24 27
desired output vector: c(1, 2, 3, 4, 5, 6, 7.....)
with code provided simple solution extract row, looks easy maybe missed something.
new_dat <- dat[1, ] new_dat [1] 1 4 7 10 13 16 19 22 25
edit
my solution works not efficient. here have improved loop versions can store objects separately in 1 command.
first define elements name of objects:
val <- c(1:3) nam <- "new_dat_"
and extract elements loop.
for(i in 1:nrow(dat)){ assign(paste(nam, val, sep = "")[i], dat[i, ]) }
after use ls()
, should see 3 elements named new_dat_1","new_dat_2", "new_dat_3" "val"
each of them contains 1 row of dat. solution can helpful if have extract several rows , not 1 , lead 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
Comments
Post a Comment