我正在尝试将数据放入数据框中:
> mymatrix <- matrix(seq(1, 12), ncol=4)
> mymatrix
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
> myresults <- c(TRUE, FALSE, TRUE)
我想将其封装在数据框中:
> df <- data.frame(mymatrix, myresults)
> df
X1 X2 X3 X4 myresults
1 1 4 7 10 TRUE
2 2 5 8 11 FALSE
3 3 6 9 12 TRUE
然而,当我试着抬头时:
> df$mymatrix
NULL
> df$
df$X1 df$X2 df$X3 df$X4 df$myresults
NULL
? df$
命名空间? 注意:我正在尝试创建类似于纱线包的数据
答案 0 :(得分:1)
你可以尝试
df <- data.frame(mymatrix=I(mymatrix), myresults)
df$mymatrix
# [,1] [,2] [,3] [,4]
#[1,] 1 4 7 10
#[2,] 2 5 8 11
#[3,] 3 6 9 12
str(df)
#'data.frame': 3 obs. of 2 variables:
#$ mymatrix : 'AsIs' int [1:3, 1:4] 1 2 3 4 5 6 7 8 9 10 ...
#$ myresults: logi TRUE FALSE TRUE
lm(mymatrix ~ myresults, data=df)
#Call:
#lm(formula = mymatrix ~ myresults, data = df)
#Coefficients:
# [,1] [,2] [,3] [,4]
#(Intercept) 2.000e+00 5.000e+00 8.000e+00 1.100e+01
#myresultsTRUE 2.040e-16 8.158e-16 8.158e-16 2.448e-15
顺便说一下,你不需要创建一个&#39; df&#39;这样做
lm(mymatrix~myresults)
#Call:
#lm(formula = mymatrix ~ myresults)
#Coefficients:
# [,1] [,2] [,3] [,4]
#(Intercept) 2.000e+00 5.000e+00 8.000e+00 1.100e+01
#myresultsTRUE 2.040e-16 8.158e-16 8.158e-16 2.448e-15