将数据帧转换为矩阵

时间:2015-10-05 06:21:00

标签: r matrix dataframe

我有一个数据框“mydata”,如下所示:

enter image description here

以下是详细信息:

  • class(mydata):data.frame
  • typeof(mydata):list
  • typeof(mydata[any element]):整数

如何将其转换为矩阵以便我可以对其执行算术运算?

我尝试使用:

  1. data.matrix(mydata),它返回了一些奇怪的整数值。
  2. as.numeric(mydata),也没有用
  3. mydata:

    structure(list(V1 = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
    9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 138L, 139L, 140L, 
    141L, 142L, 143L, 144L, 145L, 146L, 147L, 148L, 149L, 264L, 265L, 
    266L, 267L, 268L, 269L, 270L, 271L, 272L, 273L, 274L, 275L, 276L, 
    277L, 278L, 279L, 280L, 281L, 282L, 283L, 284L, 285L, 286L, 287L, 
    288L, 289L, 290L, 291L, 292L, 293L, 294L, 295L, 296L, 297L, 298L, 
    299L, 300L, 301L, 302L, 303L, 304L, 305L, 306L, 307L, 308L, 309L, 
    310L, 311L, 312L, 313L, 314L, 315L, 316L, 317L, 318L, 319L, 320L, 
    321L, 322L, 323L, 324L, 325L, 326L, 327L, 328L, 329L, 330L, 331L, 
    332L, 333L, 334L, 335L, 336L, 337L, 338L, 339L, 340L, 341L, 342L, 
    343L, 344L, 345L, 346L, 347L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 
    25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 
    38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 
    51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 
    64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 
    77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 
    90L, 91L, 92L, 93L, 94L, 95L, 96L, 97L, 98L, 99L, 100L, 101L, 
    102L, 103L, 104L, 105L, 106L, 107L, 108L, 109L, 110L, 111L, 112L, 
    113L, 114L, 115L, 116L, 117L, 118L, 119L, 120L, 121L, 122L, 123L, 
    124L, 125L, 126L, 127L, 128L, 129L, 130L, 131L, 132L, 133L, 134L, 
    135L, 136L, 137L, 150L, 151L, 152L, 153L, 154L, 155L, 156L, 157L, 
    158L, 159L, 160L, 161L, 162L, 163L, 164L, 165L, 166L, 167L, 168L, 
    169L, 170L, 171L, 172L, 173L, 174L, 175L, 176L, 177L, 178L, 179L, 
    180L, 181L, 182L, 183L, 184L, 185L, 186L, 187L, 188L, 189L, 190L, 
    191L, 192L, 193L, 194L, 195L, 196L, 197L, 198L, 199L, 200L, 201L, 
    202L, 203L, 204L, 205L, 206L, 207L, 208L, 209L, 210L, 211L, 212L, 
    213L, 214L, 215L, 216L, 217L, 218L, 219L, 220L, 221L, 222L, 223L, 
    224L, 225L, 226L, 227L, 228L, 229L, 230L, 231L, 232L, 233L, 234L, 
    235L, 236L, 237L, 238L, 239L, 240L, 241L, 242L, 243L, 244L, 245L, 
    246L, 247L, 248L, 249L, 250L, 251L, 252L, 253L, 254L, 255L, 256L, 
    257L, 258L, 259L, 260L, 261L, 262L, 263L), .Label = c("0.17", 
    "0.33", "0.50", "0.67", "0.83", "1.00", "1.08", "1.17", "1.25", 
    "1.33", "1.42", "1.50", "1.58", "1.67", "1.75", "1.83", "1.92", 
    "10.00", "10.08", "10.17", "10.25", "10.33", "10.42", "10.50", 
    "10.58", "10.67", "10.75", "10.83", "10.92", "11.00", "11.08", 
    "11.17", "11.25", "11.33", "11.42", "11.50", "11.58", "11.67", 
    "11.75", "11.83", "11.92", "12.00", "12.08", "12.17", "12.25", 
    "12.33", "12.42", "12.50", "12.58", "12.67", "12.75", "12.83", 
    "12.92", "13.00", "13.08", "13.17", "13.25", "13.33", "13.42", 
    "13.50", "13.58", "13.67", "13.75", "13.83", "13.92", "14.00", 
    "14.08", "14.17", "14.25", "14.33", "14.42", "14.50", "14.58", 
    "14.67", "14.75", "14.83", "14.92", "15.00", "15.08", "15.17", 
    "15.25", "15.33", "15.42", "15.50", "15.58", "15.67", "15.75", 
    "15.83", "15.92", "16.00", "16.08", "16.17", "16.25", "16.33", 
    "16.42", "16.50", "16.58", "16.67", "16.75", "16.83", "16.92", 
    "17.00", "17.08", "17.17", "17.25", "17.33", "17.42", "17.50", 
    "17.58", "17.67", "17.75", "17.83", "17.92", "18.00", "18.08", 
    "18.17", "18.25", "18.33", "18.42", "18.50", "18.58", "18.67", 
    "18.75", "18.83", "18.92", "19.00", "19.08", "19.17", "19.25", 
    "19.33", "19.42", "19.50", "19.58", "19.67", "19.75", "19.83", 
    "19.92", "2.00", "2.08", "2.17", "2.25", "2.33", "2.42", "2.50", 
    "2.58", "2.67", "2.75", "2.83", "2.92", "20.00", "20.08", "20.17", 
    "20.25", "20.33", "20.42", "20.50", "20.58", "20.67", "20.75", 
    "20.83", "20.92", "21.00", "21.08", "21.17", "21.25", "21.33", 
    "21.42", "21.50", "21.58", "21.67", "21.75", "21.83", "21.92", 
    "22.00", "22.08", "22.17", "22.25", "22.33", "22.42", "22.50", 
    "22.58", "22.67", "22.75", "22.83", "22.92", "23.00", "23.08", 
    "23.17", "23.25", "23.33", "23.42", "23.50", "23.58", "23.67", 
    "23.75", "23.83", "23.92", "24.00", "24.08", "24.17", "24.25", 
    "24.33", "24.42", "24.50", "24.58", "24.67", "24.75", "24.83", 
    "24.92", "25.00", "25.08", "25.17", "25.25", "25.33", "25.42", 
    "25.50", "25.58", "25.67", "25.75", "25.83", "25.92", "26.00", 
    "26.08", "26.17", "26.25", "26.33", "26.42", "26.50", "26.58", 
    "26.67", "26.75", "26.83", "26.92", "27.00", "27.08", "27.17", 
    "27.25", "27.33", "27.42", "27.50", "27.58", "27.67", "27.75", 
    "27.83", "27.92", "28.00", "28.08", "28.17", "28.25", "28.33", 
    "28.42", "28.50", "28.58", "28.67", "28.75", "28.83", "28.92", 
    "29.00", "29.17", "29.33", "29.50", "29.67", "29.83", "3.00", 
    "3.08", "3.17", "3.25", "3.33", "3.42", "3.50", "3.58", "3.67", 
    "3.75", "3.83", "3.92", "4.00", "4.08", "4.17", "4.25", "4.33", 
    "4.42", "4.50", "4.58", "4.67", "4.75", "4.83", "4.92", "5.00", 
    "5.08", "5.17", "5.25", "5.33", "5.42", "5.50", "5.58", "5.67", 
    "5.75", "5.83", "5.92", "6.00", "6.08", "6.17", "6.25", "6.33", 
    "6.42", "6.50", "6.58", "6.67", "6.75", "6.83", "6.92", "7.00", 
    "7.08", "7.17", "7.25", "7.33", "7.42", "7.50", "7.58", "7.67", 
    "7.75", "7.83", "7.92", "8.00", "8.08", "8.17", "8.25", "8.33", 
    "8.42", "8.50", "8.58", "8.67", "8.75", "8.83", "8.92", "9.00", 
    "9.08", "9.17", "9.25", "9.33", "9.42", "9.50", "9.58", "9.67", 
    "9.75", "9.83", "9.92"), class = "factor"), V2 = structure(c(55L, 
    54L, 57L, 56L, 60L, 58L, 59L, 61L, 62L, 63L, 64L, 67L, 68L, 65L, 
    66L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 75L, 76L, 77L, 78L, 79L, 
    80L, 82L, 81L, 84L, 85L, 86L, 83L, 87L, 89L, 88L, 89L, 89L, 90L, 
    91L, 92L, 94L, 93L, 96L, 95L, 97L, 97L, 98L, 99L, 101L, 100L, 
    101L, 101L, 102L, 101L, 103L, 104L, 106L, 102L, 105L, 102L, 106L, 
    107L, 108L, 108L, 109L, 111L, 111L, 111L, 109L, 111L, 113L, 110L, 
    111L, 115L, 112L, 112L, 110L, 111L, 110L, 117L, 111L, 111L, 116L, 
    120L, 117L, 119L, 113L, 118L, 114L, 123L, 121L, 123L, 124L, 122L, 
    125L, 127L, 126L, 127L, 133L, 128L, 130L, 127L, 129L, 131L, 132L, 
    131L, 132L, 133L, 134L, 135L, 138L, 140L, 137L, 136L, 139L, 141L, 
    145L, 143L, 140L, 144L, 142L, 2L, 3L, 4L, 9L, 8L, 5L, 2L, 1L, 
    146L, 3L, 4L, 9L, 10L, 6L, 14L, 9L, 14L, 9L, 13L, 7L, 10L, 17L, 
    16L, 19L, 11L, 11L, 14L, 9L, 19L, 17L, 19L, 18L, 11L, 17L, 14L, 
    12L, 19L, 14L, 19L, 15L, 22L, 27L, 25L, 19L, 21L, 24L, 22L, 23L, 
    28L, 24L, 23L, 23L, 28L, 23L, 26L, 27L, 22L, 18L, 22L, 27L, 22L, 
    23L, 20L, 14L, 22L, 19L, 22L, 14L, 15L, 27L, 14L, 14L, 28L, 29L, 
    34L, 35L, 37L, 36L, 29L, 44L, 36L, 37L, 29L, 30L, 35L, 35L, 32L, 
    34L, 31L, 32L, 31L, 34L, 36L, 39L, 33L, 35L, 34L, 31L, 38L, 44L, 
    38L, 34L, 31L, 28L, 37L, 46L, 47L, 43L, 45L, 51L, 40L, 43L, 38L, 
    42L, 46L, 45L, 49L, 52L, 45L, 44L, 45L, 51L, 45L, 45L, 48L, 45L, 
    43L, 48L, 51L, 44L, 49L, 51L, 51L, 49L, 47L, 42L, 45L, 52L, 38L, 
    25L, 38L, 39L, 45L, 47L, 45L, 44L, 45L, 51L, 46L, 43L, 51L, 45L, 
    50L, 45L, 49L, 43L, 49L, 49L, 43L, 51L, 49L, 49L, 42L, 45L, 40L, 
    52L, 51L, 42L, 45L, 36L, 37L, 52L, 44L, 44L, 48L, 43L, 44L, 48L, 
    44L, 42L, 39L, 42L, 46L, 48L, 53L, 44L, 45L, 37L, 34L, 45L, 43L, 
    40L, 43L, 43L, 51L, 43L, 45L, 40L, 41L, 44L, 42L, 40L, 40L, 43L, 
    42L, 44L, 49L, 42L, 45L, 43L, 49L, 45L, 43L, 45L, 41L, 42L, 35L, 
    45L, 42L, 42L, 39L, 37L, 45L, 36L), .Label = c("100", "100.1", 
    "100.3", "100.4", "100.5", "100.6", "100.7", "100.9", "101", 
    "101.1", "101.2", "101.3", "101.4", "101.5", "101.6", "101.7", 
    "101.8", "101.9", "102.1", "102.2", "102.3", "102.4", "102.5", 
    "102.6", "102.7", "102.8", "102.9", "103", "103.3", "103.4", 
    "103.5", "103.6", "103.7", "103.8", "103.9", "104", "104.1", 
    "104.2", "104.3", "104.4", "104.5", "104.6", "104.7", "104.8", 
    "104.9", "105", "105.1", "105.2", "105.3", "105.4", "105.5", 
    "105.6", "105.9", "77", "77.7", "78", "78.1", "78.4", "78.7", 
    "78.9", "79.1", "79.5", "80", "80.1", "80.6", "80.7", "80.8", 
    "81", "81.2", "81.5", "81.8", "82.3", "82.5", "82.7", "82.9", 
    "83.3", "84.1", "84.3", "84.4", "84.5", "84.6", "84.7", "85.3", 
    "85.5", "85.6", "85.7", "86.2", "86.3", "86.6", "87", "87.1", 
    "87.3", "87.6", "87.7", "88.3", "88.5", "88.7", "88.8", "89.3", 
    "89.5", "90.1", "90.4", "90.5", "90.6", "90.9", "91", "91.3", 
    "91.5", "92", "92.3", "92.4", "92.6", "92.7", "92.9", "93", "93.1", 
    "93.2", "93.3", "93.4", "93.5", "93.6", "93.9", "94", "94.4", 
    "94.6", "94.7", "94.9", "95.3", "95.5", "95.7", "96", "96.4", 
    "96.6", "96.9", "97.2", "97.7", "97.8", "98", "98.3", "98.4", 
    "98.5", "98.6", "98.8", "99.2", "99.5", "99.7"), class = "factor")), .Names = c("V1", 
    "V2"), class = "data.frame", row.names = c(NA, -347L))
    

1 个答案:

答案 0 :(得分:2)

原因是您使用as.matrix时数据存储为字符,原始数据框中存储的数据是因子列:

> sapply(mydata,class)
      V1       V2 
"factor" "factor" 

您可以先将它们更改为字符,然后将它们更改为数字,然后将它们设为数字(请注意:不要只使用as.numeric,因为这样会更改值!):

mydata[] <- lapply(mydata, function(x) as.numeric(as.character(x)))

现在您的数据是数字:

> sapply(mydata,class)
       V1        V2 
"numeric" "numeric" 

您现在可以使用以下内容将数据框架更改为矩阵:

mydata <- as.matrix(mydata)