首页 > 解决方案 > Putting row names and column names when converting from list to data frame

问题描述

I know that this question might be similar to the previous ones, e.g., this and this. However, I found it confusing to add the row names and column names as a result of converting from list to data frame as follows:

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

The idea is to extract coefficient hoCoef and confint from log-transform linear regression of length-weight relationship of fish. And aggregate the result into a readable data frame. From the code above I mange to extract the "raw" result:

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

So how can I get the desired output like this:

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

标签: rlistdataframeloops

解决方案


In LWfunction return a 1-row dataframe with all the required values in it.

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)
}

apply it for each year and month :

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>

推荐阅读