【发布时间】:2021-01-18 18:36:19
【问题描述】:
data1 <- structure(list(y = c(0.786495547305097, -0.275294469743402, 5.01043554797471,
3.80786336657872, 2.13918082927747), x1 = c(1.26295428488079,
-0.326233360705649, 1.3297992629225, 1.2724293214294, 0.414641434456408
), x2 = c(-0.729979080566544, -0.977769604136991, 1.60183697933767,
0.911948278954854, 0.879294677497606), x3 = c(-0.39000956273028,
-1.81922223008177, 0.659180710219526, 0.459621672673119, 1.6166263368295
)), row.names = c(NA, 5L), class = "data.frame")
data2 <- structure(list(V8 = c(-1.72904385751157, -1.07084107622855, 0.485233893853134,
-0.512561121790257, -0.584170977530818), V9 = c(-0.370376604243532,
-0.264930923072872, 0.815334082977478, -0.188695978779318, -1.58942168300819
), V10 = c(-0.154982593979532, 1.29764064456055, -0.851074132826296,
0.496249346485277, -1.72196830424353), y = structure(c(-1.33203440383281,
0.117885549592542, 4.36303461564377, 0.572854292213718, -0.627729381607459
), .Dim = c(5L, 1L), .Dimnames = list(c("1", "2", "3", "4", "5"
), NULL))), row.names = c("1", "2", "3", "4", "5"), class = "data.frame")
我有 2 个数据集。两者都属于data.frame
> class(data1)
[1] "data.frame"
> class(data2)
[1] "data.frame"
但是,data1 中的列属于numeric 类型,而data2 中的列属于matrix 类型。
> class(data1$y)
[1] "numeric"
> class(data2$y)
[1] "matrix" "array"
如何将data2 中的列转换为numeric 类,就像data1 中的列一样?
【问题讨论】: