2

I'm very new to R and coding. Trying to sort out a set of data which I obtained by:

filename = read.delim("UA0001.dat", header = FALSE, skip=16)

What I get is a matrix with one column and multiple row, where in each column there are 8 different data points.

V1

1-3.39 3.11 4.361 3.35 3.76 3.66 3.44 3.72
2-3.13 3.35 3.01 3.05 2.73 2.90 2.73 2.31
.
.
.

How can I separate all these so I can have a matrix with one column where each of these data points are placed in separate rows?

V1

1-3.39
2-3.11
3-4.36 
4-3.35
5-3.76
.
.
.
morgan121
  • 1,989
  • 1
  • 12
  • 27
AA16
  • 27
  • 2
  • Original data are separated by " " and " " in different lines. Is there any way to input two different separation arguments when reading the data? – AA16 Dec 28 '18 at 01:45
  • `read.delim("UA0001.dat", header = FALSE, skip=16, sep = " ")` should give you one value per column. – Philipp R Dec 28 '18 at 04:40

2 Answers2

0

You could just restructure it by taking the transpose and breaking it into vector and converting it back to matrix with ncol = 1

matrix(c(t(mat)), ncol = 1)


#       [,1]
# [1,] 3.390
# [2,] 3.110
# [3,] 4.361
# [4,] 3.350
# [5,] 3.760
# [6,] 3.660
# [7,] 3.440
# [8,] 3.720
# [9,] 3.130
#[10,] 3.350
#[11,] 3.010
#[12,] 3.050
#[13,] 2.730
#[14,] 2.900
#[15,] 2.730
#[16,] 2.310

data

mat <-matrix(c(3.39, 3.11, 4.361, 3.35, 3.76, 3.66, 3.44, 3.72,
 3.13 ,3.35, 3.01, 3.05, 2.73, 2.90, 2.73, 2.31), byrow = TRUE, nrow = 2)
Ronak Shah
  • 286,338
  • 16
  • 97
  • 143
0

Here is an option with unmatrix from gdata

library(gdata)
matrix(unmatrix(mat, byrow = TRUE))
#      [,1]
# [1,] 3.390
# [2,] 3.110
# [3,] 4.361
# [4,] 3.350
# [5,] 3.760
# [6,] 3.660
# [7,] 3.440
# [8,] 3.720
# [9,] 3.130
#[10,] 3.350
#[11,] 3.010
#[12,] 3.050
#[13,] 2.730
#[14,] 2.900
#[15,] 2.730
#[16,] 2.310

Or with aperm

matrix(aperm(mat, c(2, 1)))

data

mat <-matrix(c(3.39, 3.11, 4.361, 3.35, 3.76, 3.66, 3.44, 3.72,
 3.13 ,3.35, 3.01, 3.05, 2.73, 2.90, 2.73, 2.31), byrow = TRUE, nrow = 2)
akrun
  • 674,427
  • 24
  • 381
  • 486