简体   繁体   English

将奇怪的data.frame转换为R中的矩阵

[英]Converting strange data.frame to matrix in R

I have the following data.frame and convert into matrix object after deleting each delimiter. 我有以下data.frame并删除每个定界符后转换为矩阵对象。

> 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? 为此,我应该应用apply()哪些功能?

Thanks in advance Sean 预先感谢肖恩

You don't really need apply at all. 您实际上根本不需要apply 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 在基础R中,(1)您可以按行将列粘贴在一起,然后使用read.csv阅读该文本

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

Or, (2) using scan directly 或者,(2)直接使用scan

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

Or, (3) you could use cSplit from splitstackshape 或者,(3),你可以使用cSplitsplitstackshape

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

A solution based on apply : 一个基于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. 函数apply用于将函数apply数据帧的每一行。 The character vectors are split at the commas ( strsplit ). 字符向量在逗号处分隔( strsplit )。 This returns a list. 这将返回一个列表。 This list is converted to a vector with unlist . 此列表将转换为带有unlist的向量。 Next, as.numeric is used to transform the character vector to a numeric vector. 接下来,使用as.numeric将字符向量转换为数字向量。 The function apply returns a matrix in which a column corresponds to a row in the original data frame. apply函数返回一个矩阵,其中一列对应于原始数据帧中的一行。 Finally, the function t is used to transpose the matrix. 最后,函数t用于转置矩阵。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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