简体   繁体   中英

Converting strange data.frame to matrix in R

I have the following data.frame and convert into matrix object after deleting each delimiter.

> data
  ID        COL1        COL2        COL3        COL4         COL5
1  1     1,2,3,4     5,6,7,8  9,10,11,12 13,14,15,16  17,18,19,20
2  2 11,12,13,14 15,16,17,18 19,20,21,22 23,24,25,26  27,28,29,30
3  3 21,22,23,24 25,26,27,28 29,30,31,32 33,34,35,36  37,38,39,40
4  4 31,32,33,34 35,36,37,38 39,40,41,42 43,44,45,46  47,48,49,50
5  5 41,42,43,44 45,46,47,48 49,50,51,52 53,54,55,56  57,58,59,60
6  6 51,52,53,54 55,56,57,58 59,60,61,62 63,64,65,66  67,68,69,70
7  7 61,62,63,64 65,66,67,68 69,70,71,72 73,74,75,76  77,78,79,80
8  8 71,72,73,74 75,76,77,78 79,80,81,82 83,84,85,86  87,88,89,90
9  9 81,82,83,84 85,86,87,88 89,90,91,92 93,94,95,96 97,98,99,100

===>

> data.new
  [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20]  [,21]
1   1    1    2    3    4    5    6    7    8     9    10    11    12    13    14    15    16    17    18    19     20
2   2   11   12   13   14   15   16   17   18    19    20    21    22    23    24    25    26    27    28    29     30
3   3   21   22   23   24   25   26   27   28    29    30    31    32    33    34    35    36    37    38    39     40
4   4   31   32   33   34   35   36   37   38    39    40    41    42    43    44    45    46    47    48    49     50
5   5   41   42   43   44   45   46   47   48    49    50    51    52    53    54    55    56    57    58    59     60
6   6   51   52   53   54   55   56   57   58    59    60    61    62    63    64    65    66    67    68    69     70
7   7   61   62   63   64   65   66   67   68    69    70    71    72    73    74    75    76    77    78    79     80
8   8   71   72   73   74   75   76   77   78    79    80    81    82    83    84    85    86    87    88    89     90
9   9   81   82   83   84   85   86   87   88    89    90    91    92    93    94    95    96    97    98    99    100

To do this, what functions of apply() should I apply?

Thanks in advance Sean

You don't really need apply at all. You can re-read the data. Try any of these three possibilities.

In base R, (1) you could paste the columns together by row then read that text with read.csv

dc <- do.call(paste, c(data, list(sep = ",")))
unname(as.matrix(read.csv(text = dc, header = FALSE)))

Or, (2) using scan directly

matrix(scan(text = dc, what = integer(), sep = ","), length(dc), byrow = TRUE)

Or, (3) you could use cSplit from splitstackshape

library(splitstackshape)
unname(as.matrix(cSplit(data, 2:6)))

A solution based on apply :

t(apply(data, 1, function(x) as.numeric(unlist(strsplit(x, ",")))))

How it works?

The function apply is used to apply a function to each row of the data frame. The character vectors are split at the commas ( strsplit ). This returns a list. This list is converted to a vector with unlist . Next, as.numeric is used to transform the character vector to a numeric vector. The function apply returns a matrix in which a column corresponds to a row in the original data frame. Finally, the function t is used to transpose the matrix.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM