【问题标题】:Put Correlation Output of Function into Data Frame将函数的相关输出放入数据框中
【发布时间】:2017-12-25 07:26:14
【问题描述】:

随着时间的推移,我正在执行大量人口的相关性。我已将它们相应地分开,并通过 lapply 的函数将它们放在了一起。我想将每个相关性的输出放入一个数据框中(即:每一行将是一个相关性的信息,列:correlation's name, p-valuet 统计量dfCIscorcoeff)。

我有两个问题:

  1. 我不知道如何提取拆分中所做的关联名称
  2. 我可以让我的函数在拆分上运行相关性(600+ 相关性),但我不能让它打印到数据框中。澄清一下:当我在没有循环的情况下运行该函数时,它会为每个组执行所有 600 个相关性。但是,当我添加循环时,它会为拆分中的所有组生成 NULL。

这是我目前所拥有的:

> head(Birds) #Shortened for this Post
Location      Species   Year Longitude Latitude Section Total Percent  Family
1 Chiswell A  Kittiwake 1976 -149.5847 59.59559 Central   310 16.78397 Gull

BigSplit<-split(Birds,list(Birds$Family, Birds$Location, 
Birds$Section,Birds$Species), drop=T) #A list of Dataframes

#Make empty data frame
resultcor <- data.frame(Name = character(),
                        tvalue = character(),
                        degreeF = character(),
                        pvalue = character(),
                        CIs = character(),
                        corcoeff = character(),stringsAsFactors = F)

WorkFunc <- function(dataset) {
     data.name = substitute(dataset) #Use "dataset" as substitute for actual dataset name

     #Correlation between Year and population Percent
     try({
          correlation <- cor.test(dataset$Year, dataset$Percent, method = "pearson")    
     }, silent = TRUE)

     for (i in 1:nrow(resultcor)) {
          resultcor$Name[i] <- ??? #These ??? are not in the code, just highlighting Issue 1
          resultcor$tvalue[i] <- correlation$dataset$statistic
          resultcor$degreeF[i] <- correlation$dataset$parameter
          resultcor$pvalue[i] <- correlation$dataset$p.value
          resultcor$CIs[i] <- correlation$dataset$conf.int
          resultcor$corcoeff[i] <- correlation$dataset$estimate
     }
}

lapply(BigSplit, WorkFunc)

任何帮助将不胜感激,谢谢!

【问题讨论】:

  • 查看包裹 broom 它会为您完成所有这些工作。
  • split 是在哪里进行的?请显示该代码。 我无法将其打印到数据框中...请解释发生了什么。 BigSplit 是什么数据帧列表?
  • @Parfait 为了清楚起见,我进行了编辑。是的 BigSplit 数据框列表。谢谢
  • @sinQueso 扫帚包正是我想要的,有没有办法将“数据名称”添加为列之一,然后将它们与函数一起添加?
  • @LearningTheMacros 看看这本书的章节r4ds.had.co.nz/many-models.html 它经历了你正在尝试做的事情

标签: r function loops dataframe correlation


【解决方案1】:

考虑使用Mapmapply 的变体)来传递BigSplit 的元素和名称。使用Map 将输出一个数据框列表,然后您可以在末尾使用do.call() 进行行绑定。下面假设 BigSplit 是一个命名列表。

WorkFunc <- function(dataset, dataname) {
    # Correlation between Year and population Percent
    tryCatch({ 
        correlation <- cor.test(dataset$Year, dataset$Percent, method = "pearson")
        CIs <- correlation$conf.int

        return(data.frame(
                  Name = dataname,
                  tvalue = correlation$statistic,
                  degreeF = correlation$parameter,
                  pvalue = correlation$p.value,
                  CI_lower = ifelse(is.null(CIs), NA, CIs[[1]]),
                  CI_higher = ifelse(is.null(CIs), NA, CIs[[2]]),
                  corcoeff = correlation$estimate
             )
         ) 
     }, error = function(e) 
             return(data.frame(
                        Name = character(0),
                        tvalue = numeric(0),
                        degreeF = numeric(0),
                        pvalue = numeric(0),
                        CI_lower = numeric(0),
                        CI_higher = numeric(0),
                        corcoeff = numeric(0)
                    )
              )
      )
}    

# BUILD CORRELATION DATAFRAMES INTO LIST
cor_df_list <- Map(WorkFunc, BigSplit, names(BigSplit))
cor_df_list <- mapply(WorkFunc, BigSplit, names(BigSplit), SIMPLIFY=FALSE)   # EQUIVALENT

# ROW BIND ALL DATAFRAMES TO FINAL LARGE DATAFRAME
finaldf <- do.call(rbind, cor_df_list)

【讨论】:

  • 我遵循了这段代码,但我似乎得到了这个错误:data.frame 中的错误(名称 = 数据名,tvalue = 相关 $ 统计,度 F = 相关 $ 参数,:参数意味着不同的行数: 1, 0 另外: 警告消息: 1: 在 data.frame(Name = dataname, tvalue = correlation$statistic, degreeF = correlation$parameter, : 行名是从一个短变量中找到并被丢弃 2: 在数据中.frame(Name = dataname, tvalue = correlation$statistic, degreeF = correlation$parameter, : 行名是从一个短变量中找到的,已被丢弃
  • 道歉。我忘了在data.frame() 中添加逗号来分隔值。
  • 哦,不,我也遇到了这个错误,但是我理解并添加了逗号,这个错误是在添加了逗号之后。
  • 查看更新。 CI实际上是一个包含两个较低和较高的列表。我添加了这样的列。另一个原因也可能是您的关联失败。我注意到你使用try
  • 我添加了一个tryCatch() 以在出错时返回一个空数据帧。拆分 dfs 可能只有一行!
猜你喜欢
  • 2020-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-03
  • 2023-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多