简体   繁体   中英

How to convert matrix to data frame in R?

This is what the sample looks like:

      start_location_Long start_location_Lat end_location_Long end_location_Lat start_time1_cos end_time1_cos clusterNum
 [1,]          0.52378920          2.4368453        0.54442969        2.4795016      -0.8556297    -0.9205550          6
 [2,]          0.52067466          2.4344570       -0.19032110        1.0495159       0.9838412     3.4135479          5
 [3,]          0.15442785          1.9243047        0.22881591        1.6337213      -0.0616968    -0.8731549          6
 [4,]          0.40289891          0.5148578        0.46360470        1.2610018      -0.3169268    -0.2686603          6
 [5,]          0.04622642          1.0748470        0.12649364        1.0431320      -0.7774222    -0.7788368          6
 [6,]          0.63314416          0.9234254        0.14450812        1.0302006      -0.8225888    -0.8123482          6
 [7,]          0.68966729          0.6019531        1.21697046        0.9342789      -0.7434470     0.4569955          4
 [8,]          2.32099405          0.9297944        2.14687817       -0.6940977      -0.8340856    -0.6665446          4
 [9,]          0.06549043         -0.5369730        0.07749424       -0.5310636      -0.3612566    -0.4558744          8
[10,]         -0.32832583          0.0215832       -0.23823963       -0.3066462       0.8659375     1.0576191          7
out <- structure(c(0.523789201116763, 0.520674661270142, 0.154427845973528,0.402898913736964, 0.0462264246354297, 0.633144155731261, 0.68966728628088,2.3209940459422, 0.0654904303534412, -0.328325830252011, 2.43684534992552,2.43445699655717, 1.92430471707559, 0.514857782627747, 1.07484703572908,0.923425432175079, 0.601953068793905, 0.929794374490717, -0.536973040797758,0.0215832002825539, 0.544429688416353, -0.190321103194908, 0.228815912614685,0.463604699407898, 0.126493638268144, 0.144508123188246, 1.21697045877012,2.14687817035007, 0.0774942392852146, -0.238239633082625, 2.4795015924936,1.04951586212972, 1.63372134194528, 1.26100184697751, 1.04313199726203,1.03020057868387, 0.93427891682567, -0.694097690965059, -0.53106360357468,-0.306646200148732, -0.855629693492673, 0.983841170626353, -0.0616968018017336,-0.316926796024306, -0.777422178678194, -0.82258880499052, -0.743446969246078,-0.834085583349327, -0.361256556045013, 0.865937517514796, -0.920554961834621,3.41354789197562, -0.873154942000389, -0.268660340896956, -0.778836788633205,-0.812348178573314, 0.456995511946368, -0.666544551696315, -0.455874395664872,1.05761911141203, 6, 5, 6, 6, 6, 6, 4, 4, 8, 7), .Dim = c(10L,7L), .Dimnames = list(NULL, c("start_location_Long", "start_location_Lat","end_location_Long", "end_location_Lat", "start_time1_cos", "end_time1_cos","clusterNum"))) 

So, I want to convert this large matrix to a data frame because I want to export this data into CSV format later on.

My expected result should look like this;

start_location_Long start_location_Lat end_location_Long end_location_Lat start_time1_cos end_time1_cos clusterNum
       <dbl>              <dbl>             <dbl>            <dbl>           <dbl>         <dbl>         <dbl>
   0.52378920          2.4368453        0.54442969        2.4795016      -0.8556297    -0.9205550          6
   0.52067466          2.4344570       -0.19032110        1.0495159       0.9838412     3.4135479          5
   0.15442785          1.9243047        0.22881591        1.6337213      -0.0616968    -0.8731549          6
   0.40289891          0.5148578        0.46360470        1.2610018      -0.3169268    -0.2686603          6
   0.04622642          1.0748470        0.12649364        1.0431320      -0.7774222    -0.7788368          6
   0.63314416          0.9234254        0.14450812        1.0302006      -0.8225888    -0.8123482          6
   0.68966729          0.6019531        1.21697046        0.9342789      -0.7434470     0.4569955          4
   2.32099405          0.9297944        2.14687817       -0.6940977      -0.8340856    -0.6665446          4
   0.06549043         -0.5369730        0.07749424       -0.5310636      -0.3612566    -0.4558744          8
  -0.32832583          0.0215832       -0.23823963       -0.3066462       0.8659375     1.0576191          7

Thank you in advance.

The expected output is a tibble . So, we can use as_tibble

library(tibble)
out1 <- as_tibble(out)

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