Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.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
使用lappy和purrr::map索引变量_R - Fatal编程技术网

使用lappy和purrr::map索引变量

使用lappy和purrr::map索引变量,r,R,我有这个数据框: dataset=structure(list(var1 = c(28.5627505742013, 22.8311421908438, 95.2216156944633, 43.9405107684433, 97.11211245507, 48.4108281508088), var2 = c(32.9009465128183, 54.1136392951012, 69.3181485682726, 70.2100433968008, 44.0986660309136,

我有这个
数据框

dataset=structure(list(var1 = c(28.5627505742013, 22.8311421908438, 
95.2216156944633, 
43.9405107684433, 97.11211245507, 48.4108281508088), var2 = c(32.9009465128183, 
54.1136392951012, 69.3181485682726, 70.2100433968008, 44.0986660309136, 
62.8759404085577), var3 = c(89.6971945464611, 67.174579706043, 
37.0924087055027, 87.7977314218879, 29.3221596442163, 37.5143952667713
), var4 = c(41.5336912125349, 98.2095112837851, 80.7970978319645, 
91.1278881691396, 66.4086666144431, 69.2618868127465), var5 = c(33.9312525652349, 
88.1815139763057, 98.4453701227903, 25.0217059068382, 41.1195872165263, 
37.0983888953924), var6 = c(39.813664201647, 80.6405956856906, 
30.0273275375366, 34.6203793399036, 96.5195455029607, 44.5830867439508
), kmeans = structure(c(2L, 1L, 3L, 1L, 3L, 1L), .Label = c("1", 
"2", "3"), class = "factor")), .Names = c("var1", "var2", "var3", 
"var4", "var5", "var6", "kmeans"), row.names = c(NA, 6L), class = "data.frame")
data.frame
中使用
lappy
purrr::map
,结果正常。见:

lapply(dataset[c(1:6)],shapiro.test)

purrr::map(dataset[c(1:6)],shapiro.test)
嗯。现在,我想将其应用到列表中:

创建列表(
mylist
):

之后,在
lappy
purrr::map
中应用此函数:

lapply(mylist[c(1:6)],f)
#Error: is.numeric(x) is not TRUE

purrr::map(mylist[c(1:6)],f)
#Error: is.numeric(x) is not TRUE
试试这个:

lapply(mylist[c(1:6)],function(x){
    lapply(x,shapiro.test)
})
#Error: is.numeric(x) is not TRUE 

lapply(mylist[c(1:6)],function(x){
  lapply(x,f)
})
#Error in apply(x, 2, shapiro.test) : dim(X) must have a positive length 

mylist[c(1:6)]%>%
  map(~map(.,shapiro.test))
#Error: is.numeric(x) is not TRUE

mylist[c(1:6)]%>%
  map(~map(.,f))
#Error in apply(x, 2, shapiro.test) : dim(X) must have a positive length

怎么了?

应用
文档说明:

如果X不是数组,而是具有非空dim的类的对象 值(如数据帧),应用尝试将其强制到数组 如果是二维(如数据帧),则通过as.matrix或通过 as.array

由于每个数据帧的最后一列是一个因子(列
集群
),因此
as.matrix
调用将整行强制为字符向量(不接受该向量作为
shapiro.test
的输入)

如果在
apply
函数中仅选择数字列,则该功能将起作用

f<-function(x){
  apply(x[c(1:6)],2 , shapiro.test)
}

lapply(mylist, f)
f
lapply(mylist[c(1:6)],function(x){
    lapply(x,shapiro.test)
})
#Error: is.numeric(x) is not TRUE 

lapply(mylist[c(1:6)],function(x){
  lapply(x,f)
})
#Error in apply(x, 2, shapiro.test) : dim(X) must have a positive length 

mylist[c(1:6)]%>%
  map(~map(.,shapiro.test))
#Error: is.numeric(x) is not TRUE

mylist[c(1:6)]%>%
  map(~map(.,f))
#Error in apply(x, 2, shapiro.test) : dim(X) must have a positive length
f<-function(x){
  apply(x[c(1:6)],2 , shapiro.test)
}

lapply(mylist, f)