Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R不是矩阵,但它赢了';我也不接受数据帧_R_Dataframe_R Raster - Fatal编程技术网

R不是矩阵,但它赢了';我也不接受数据帧

R不是矩阵,但它赢了';我也不接受数据帧,r,dataframe,r-raster,R,Dataframe,R Raster,我整天都在和这只野兽搏斗。一方面它需要一个矩阵,另一方面它需要一个data.frame,但两者都不需要。请帮忙 > Coffee <- raster("b2boolcafetst.rst") > b <- raster("b2_dstcabtst.rst") > c <- raster("b2_dstrdstst.rst") > d <- raster("b2_dstfuentst.rst") > e <- raster("b2_srt

我整天都在和这只野兽搏斗。一方面它需要一个矩阵,另一方面它需要一个data.frame,但两者都不需要。请帮忙

> Coffee <- raster("b2boolcafetst.rst")
> b <- raster("b2_dstcabtst.rst")
> c <- raster("b2_dstrdstst.rst")
> d <- raster("b2_dstfuentst.rst")
> e <- raster("b2_srtmtst.rst")
> fdf <- as.data.frame(stack(Coffee, b, c, d, e))
> str(fdf)
'data.frame':   296856 obs. of  5 variables:
 $ b2boolcafetst: num  0 0 0 0 0 0 0 0 0 0 ...
 $ b2_dstcabtst : num  9512 9482 9452 9422 9392 ...
 $ b2_dstrdstst : num  1980 1980 1981 1982 1984 ...
 $ b2_dstfuentst: num  5155 5134 5112 5091 5070 ...
 $ b2_srtmtst   : num  975 980 984 991 998 ...
> fdfdm <- as.matrix(fdf)
> str(fdfdm)
 num [1:296856, 1:5] 0 0 0 0 0 0 0 0 0 0 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:5] "b2boolcafetst" "b2_dstcabtst" "b2_dstrdstst" "b2_dstfuentst" ...
> fdfmod <- glm(Coffee ~ b + c + d + e, family=binomial(link='logit'), 
+              data=fdf)
Error in model.frame.default(formula = Coffee ~ b + c + d + e, data = fdf,  : 
  object is not a matrix
> fdfmod <- glm(Coffee ~ b + c + d + e, family=binomial(link='logit'), 
+              data=fdfdm)
Error in model.frame.default(formula = Coffee ~ b + c + d + e, data = fdfdm,  : 
  'data' must be a data.frame, not a matrix or an array
> 
>咖啡b c d e fdf str(fdf)
“数据帧”:296856 obs。共有5个变量:
$b2boolcafetst:数量为0。。。
$B2dstcabtst:num 9512 9482 9452 9422 9392。。。
$B2dstrdst:num 1980 1981 1982 1984。。。
$B2dstfuentst:num 5155 5134 5112 5091 5070。。。
$b2_srtmtst:num 975980 984 991 998。。。
>FDM街(FDM)
num[1:296856,1:5]0。。。
-属性(*,“dimnames”)=2个列表
..$:空
..$:chr[1:5]“b2boolcafest”“b2u-dstcabtst”“b2-dstrdst”“b2-dstfuentst”。。。
>fdfmod fdfmod

当您引用的对象/变量不是正在使用的数据框或矩阵中的列时,往往会发生此错误。所以用数据中的列名替换“b+c+d+e”应该可以解决这个问题。所以试试这个:

glm(Coffee ~ b2_dstcabtst + b2_dstrdstst + b2_dstfuentst + b2_srtmtst, 
    family=binomial(link='logit'), data=fdf)
但是,如果要使用数据中的所有列(如此处所示),则还可以使用:

glm(Coffee ~ ., family=binomial(link='logit'), data=fdf)

使用
as.Data.Frame(x)
将对象强制到数据帧。在这种情况下:

fdfdm <- as.data.frame(fdfdm)

fdfdm将
Coffee~b+c+d+e
替换为相应的名称
b2boolcafestt
b2_u-dstabtst
,等等。我从来没有想到过这个解决方案,但它起了作用;对于那些好奇的人来说,它是data.frame的解决方案,而不是矩阵的解决方案。非常感谢你!Peter@user236260-没问题,很高兴我能帮忙。请选择此选项作为解决方案,以帮助将来有相同问题的其他人更快地找到答案。