【问题标题】:Putting row names and column names when converting from list to data frame从列表转换为数据框时放置行名和列名
【发布时间】:2021-07-14 05:54:06
【问题描述】:

我知道这个问题可能与之前的问题类似,例如thisthis。但是,由于从列表转换为数据框,我发现添加行名和列名令人困惑,如下所示:

Library("FSA", "FSAdata")
data("RuffeSLRH92")
str(RuffeSLRH92)

ruffe2 <- Subset(RuffeSLRH92,!is.na(weight) & !is.na(length))

ruffe2$logL <- log(ruffe2$length)
ruffe2$logW <- log(ruffe2$weight)

data <- Subset(ruffe2,logW >= -0.5)

LWfunction <- function(x) {
  fits <- lm(log(weight) ~ log(length), data = x)
  a <- hoCoef(fits, 2,3)
  b <- confint(fits)
  output <- list(a, b)
  return(output)
}

output <- by(data[c("weight", "length")], data[c("month", "year")], LWfunction)

df <- data.frame(matrix(unlist(output), nrow=7, byrow=TRUE),stringsAsFactors=FALSE)
df

这个想法是从鱼的长度-重量关系的对数变换线性回归中提取系数hoCoefconfint。并将结果聚合成一个可读的数据框。从上面的代码中,我设法提取“原始”结果:

X1 X2       X3         X4          X5  X6           X7        X8       X9       X10
1  2  3 3.000857 0.03958601  0.02164589  58 9.828047e-01 -11.60960 2.921617 -10.86960
2  2  3 2.880604 0.03154619 -3.78478744  64 3.415156e-04 -10.94504 2.817584 -10.35515
3  2  3 2.859603 0.03171993 -4.42615042 152 1.821503e-05 -10.92607 2.796934 -10.33690
4  2  3 2.865718 0.01889957 -7.10501173 147 4.811825e-11 -10.74430 2.828368 -10.39930
5  2  3 2.893662 0.03124268 -3.40362699  67 1.126571e-03 -11.01110 2.831301 -10.45753
6  2  3 3.022135 0.03257380  0.67954496 114 4.981701e-01 -11.67896 2.957607 -11.08538
7  2  3 2.996446 0.03140263 -0.11316551  64 9.102536e-01 -11.51532 2.933712 -10.94305
X11
1 3.080097
2 2.943625
3 2.922272
4 2.903068
5 2.956022
6 3.086664
7 3.059180

那么我怎样才能得到这样的期望输出:

year month term Ho Value Estimate Std. Error  T  df p-value 2.5% 97.5% 

【问题讨论】:

    标签: r list dataframe loops


    【解决方案1】:

    LWfunction 中返回一个包含所有必需值的 1 行数据框。

    library(FSA)
    library(FSAdata)
    library(dplyr)
    library(tidyr)
    
    LWfunction <- function(x) {
      fits <- lm(log(weight) ~ log(length), data = x)
      a <- hoCoef(fits, 2,3)
      b <- confint(fits)
      output <- cbind(a, data.frame(intercept_2.5 = b[1, 1],
                                    intercept_97.5 = b[1, 2], 
                                    log_length_2.5 = b[2, 1], 
                                    log_length_97.5 = b[2, 2]))
      return(output)
    }
    

    为每个yearmonth 应用它:

    result <- data %>%
                group_by(month, year) %>%
                summarise(output = list(LWfunction(cur_data()))) %>%
                ungroup %>%
                unnest(output)
    
    result
    # A tibble: 7 x 13
    #  month  year  term `Ho Value` Estimate `Std. Error`       T    df
    #  <int> <int> <dbl>      <dbl>    <dbl>        <dbl>   <dbl> <dbl>
    #1     4  1992     2          3     3.00       0.0396  0.0216    58
    #2     5  1992     2          3     2.88       0.0315 -3.78      64
    #3     6  1992     2          3     2.86       0.0317 -4.43     152
    #4     7  1992     2          3     2.87       0.0189 -7.11     147
    #5     8  1992     2          3     2.89       0.0312 -3.40      67
    #6     9  1992     2          3     3.02       0.0326  0.680    114
    #7    10  1992     2          3     3.00       0.0314 -0.113     64
    # … with 5 more variables: `p value` <dbl>, intercept_2.5 <dbl>,
    #   intercept_97.5 <dbl>, log_length_2.5 <dbl>,
    #   log_length_97.5 <dbl>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 2017-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-20
      相关资源
      最近更新 更多