我试图将数据放在数据帧中: 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, m
> 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
>为什么df $mymatrix为NULL?
>为什么mymatrix的列’泄漏’进入df $命名空间?
注意:我正在尝试创建类似于纱线包的数据
你可以试试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
顺便说一句,你不需要创建’df’来做到这一点
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