Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.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 - Fatal编程技术网

什么';R中的一个好策略是以列表形式存储结果,以便以后通过名称而不是索引访问它?

什么';R中的一个好策略是以列表形式存储结果,以便以后通过名称而不是索引访问它?,r,R,当我应用某些建模技术时,我想自动化结果创建过程。因此,我将使用不同的参数(例如,层次聚类距离和链接方法)。结果将有一个矩阵形式,以便我可以通过指定模型参数(例如,单,欧几里德)访问单个结果。在数据帧中,我可以简单地通过df[rname[1],cname[1]]命名列和行并访问元素。据我所知,使用数据框对象存储列表结果是不可能的。所以我需要列表来存储列表结果。但在列表中,我只能指定lst$cname[1],不能同时指定这两个维度。我说得对吗 # data frame layout for nume

当我应用某些建模技术时,我想自动化结果创建过程。因此,我将使用不同的参数(例如,层次聚类距离和链接方法)。结果将有一个矩阵形式,以便我可以通过指定模型参数(例如,单,欧几里德)访问单个结果。在数据帧中,我可以简单地通过
df[rname[1],cname[1]]
命名列和行并访问元素。据我所知,使用数据框对象存储列表结果是不可能的。所以我需要列表来存储列表结果。但在列表中,我只能指定
lst$cname[1]
,不能同时指定这两个维度。我说得对吗

# data frame layout for numeric results does not work with list results
rname<-c("u","v","w")
cname<-c("ave","single")

# dataframe for results but does not work for results which are lists
paste.1<-function(x,y) paste(x,y,sep=".")
df1<-data.frame(lapply(cname,paste.1,x=rname),row.names=rname)
colnames(df1)<-cname

# creating list for results - do not get a good idea to proceed from here Advices??
lst<-(lapply(cname,paste.1,x=rname))
names(lst)<-cname

# results example - could be anything else 
# with a dataframe I could use df1[rname,cname]<-foo(rname,cname)
# with lists I guess its not as easy
require(graphics)
ave.u <- hclust(dist(USArrests,"euclidean"), cname[1])
ave.v <- hclust(dist(USArrests,"maximum"), cname[1])
ave.w <- hclust(dist(USArrests,"manhattan"), cname[1])
single.u <- hclust(dist(USArrests,"euclidean"), cname[2])
single.v <- hclust(dist(USArrests,"maximum"), cname[2])
single.w <- hclust(dist(USArrests,"manhattan"), cname[2])
#数字结果的数据框布局不适用于列表结果

rname一个人可以有一个列表矩阵:

nr <- length(rname)
nc <- length(cname)

m <- matrix(list(), nr, nc, dimnames = list(rname, cname))

m[["u", "ave"]] <- ave.u

# etc.
我们可以访问如下元素:

> m[["euclidean", "single"]]

Call:
hclust(d = dist(USArrests, g[i, 1]), method = g[i, 2])

Cluster method   : single 
Distance         : euclidean 
Number of objects: 50

您可以重复使用
$
运算符,例如:

mname <-c("euclidean","maximum","manhattan")
lst <- sapply(mname,function(x) sapply(cname,function(y) hclust(dist(USArrests,x),y),simplify=F),simplify=F)
names(lst)<-rname
不幸的是,
lst$rname[1]$cname[1]
不起作用,但您可以使用
do.call

do.call(`$`,list(do.call(`$`,list(lst,rname[1])),cname[1]))

Call:
hclust(d = dist(USArrests, x), method = y)

Cluster method   : average 
Distance         : euclidean 
Number of objects: 50 
编辑

实际上有一个更简单的版本,但它会磨损你的方括号键

lst[[rname[1]]][cname[1]]
$ave

Call:
hclust(d = dist(USArrests, x), method = y)

Cluster method   : average 
Distance         : euclidean 
Number of objects: 50 
编辑2

上面的方括号符号似乎将对象包装在一个
列表中,这意味着它没有正确返回对象,但hadley在注释中的建议更清晰,避免了这个问题:

lst[[c(rname[1],cname[1])]]

数据帧是典型的列表,但具有“矩形”格式。列表中的每个条目都可以包含一个列表。因此,您可能希望考虑实现的一个想法是“嵌套表”。嵌套表格包含包含“主要”区别特征/类别的列,第二列包含相应的数据。我提供了一个示例,展示了嵌套表的创建,以及可以用来操作/提取内容的一些内容

    library(dplyr)
    library(tidyr)
    #mock_data creation
    samples <- c("Class A", "Class A", "Class B", "Class B")
    ht.in <- c(65, 66, 72, 75)
    wt.lb <- c(150, 160, 180, 210)
    BMI <- c(22, 24, 23, 26)
    data <- cbind.data.frame(samples, ht.in, wt.lb)
    View(data)
    #nesting data
    data_n <- data %>% group_by(samples) %>% nest() #%>% is a piping function. Takes the output from the left and pipes it into the function on the right. 
    #nested retrieval
    unnest(data_n)
    #Basic summary
    data_sum <- data %>% group_by(samples) %>% 
    summarise(mean.ht = mean(ht.in), 
      mean.lb = mean(wt.lb), mean.BMI = mean(BMI))
    #Combining summary with nested format 
    data_n_sum <- left_join(data_n, data_sum, by = "samples")
    #Extracting information via filtering and unnest()
    filter(data_n_sum, mean.ht < 70) %>%  #we filter our data_n_sum where the mean height is less than 70.
      select(samples, data) %>% #unnest only likes two columns, so we keep the two columns, one which has the nested info, and the other which contains the 'categorical' variable that belongs to the samples... 
        unnest()
    #Extracting information through $ and [[]]
    data_n_sum$data[[1]]$ht.in #You can loop through these numerical indices (lapply)
库(dplyr)
图书馆(tidyr)
#模拟数据创建

示例您是否可以将其实际制作成数据帧?据我所知,数据帧必须是矩形的,而列表可以是任意尺寸的。r中的AFAIK数据帧被实现为相同长度的列表。我认为这个问题非常令人困惑,尤其是这个示例。清楚地描述你正在努力完成的事情会有很大帮助。使用
lappy
,而不是
sapply(…,simplify=F)
。您还可以向
[[
lst[[c(“u”,“ave”)]]
@hadley我最初使用
lappy
,但最终使用
sapply
,因为它会自动设置名称,并且使用
simplify=F
基本上与
lappy
相同。使用
向量很好[[
阅读起来更加清晰,我发布的方法将该项包装在
列表中,这样它就不会返回
hclust
对象。谢谢。这正是我所寻找的解决方案。帮助很大。只需玩一下就可以了解更多的apply函数族。使用lst[[c(rname[1],cname[1])永远不会找到解决方案我自己。这对我帮助很大。有趣的解决方案,这是我一直在想的,但无法找到使其工作的方法。expand.grid()很高兴知道。该解决方案看起来类似于James的建议,但不知怎的,我对类似于矩阵的结构感到更舒服。谢谢
lst[[c(rname[1],cname[1])]]
    library(dplyr)
    library(tidyr)
    #mock_data creation
    samples <- c("Class A", "Class A", "Class B", "Class B")
    ht.in <- c(65, 66, 72, 75)
    wt.lb <- c(150, 160, 180, 210)
    BMI <- c(22, 24, 23, 26)
    data <- cbind.data.frame(samples, ht.in, wt.lb)
    View(data)
    #nesting data
    data_n <- data %>% group_by(samples) %>% nest() #%>% is a piping function. Takes the output from the left and pipes it into the function on the right. 
    #nested retrieval
    unnest(data_n)
    #Basic summary
    data_sum <- data %>% group_by(samples) %>% 
    summarise(mean.ht = mean(ht.in), 
      mean.lb = mean(wt.lb), mean.BMI = mean(BMI))
    #Combining summary with nested format 
    data_n_sum <- left_join(data_n, data_sum, by = "samples")
    #Extracting information via filtering and unnest()
    filter(data_n_sum, mean.ht < 70) %>%  #we filter our data_n_sum where the mean height is less than 70.
      select(samples, data) %>% #unnest only likes two columns, so we keep the two columns, one which has the nested info, and the other which contains the 'categorical' variable that belongs to the samples... 
        unnest()
    #Extracting information through $ and [[]]
    data_n_sum$data[[1]]$ht.in #You can loop through these numerical indices (lapply)