嵌套 data.frame

nested data.frame

我有一个嵌套的 data.frame

dput(res)
    structure(list(date = structure(list(pretty = "12:00 PM CDT on August 14,          2015", 
    year = "2015", mon = "08", mday = "14", hour = "12", min = "00", 
    tzname = "America/Chicago"), .Names = c("pretty", "year", 
    "mon", "mday", "hour", "min", "tzname"), class = "data.frame", row.names =    1L), 
    fog = "0", rain = "1", snow = "0", snowfallm = "0.00", snowfalli = "0.00", 
    monthtodatesnowfallm = "", monthtodatesnowfalli = "", since1julsnowfallm = "", 
    since1julsnowfalli = "", snowdepthm = "", snowdepthi = "", 
    hail = "0", thunder = "0", tornado = "0", meantempm = "26", 
    meantempi = "79", meandewptm = "17", meandewpti = "63", meanpressurem =   "1019", 
    meanpressurei = "30.09", meanwindspdm = "11", meanwindspdi = "7", 
    meanwdire = "", meanwdird = "139", meanvism = "16", meanvisi = "10", 
    humidity = "", maxtempm = "32", maxtempi = "90", mintempm = "21", 
    mintempi = "69", maxhumidity = "86", minhumidity = "36", 
    maxdewptm = "18", maxdewpti = "65", mindewptm = "15", mindewpti = "59", 
    maxpressurem = "1021", maxpressurei = "30.15", minpressurem = "1017", 
    minpressurei = "30.04", maxwspdm = "19", maxwspdi = "12", 
    minwspdm = "0", minwspdi = "0", maxvism = "16", maxvisi = "10", 
    minvism = "16", minvisi = "10", gdegreedays = "29", heatingdegreedays = "0", 
    coolingdegreedays = "14", precipm = "0.00", precipi = "0.00", 
    precipsource = "", heatingdegreedaysnormal = "",           monthtodateheatingdegreedays = "", 
    monthtodateheatingdegreedaysnormal = "", since1sepheatingdegreedays = "", 
    since1sepheatingdegreedaysnormal = "", since1julheatingdegreedays = "", 
    since1julheatingdegreedaysnormal = "", coolingdegreedaysnormal = "", 
    monthtodatecoolingdegreedays = "", monthtodatecoolingdegreedaysnormal = "", 
    since1sepcoolingdegreedays = "", since1sepcoolingdegreedaysnormal = "", 
    since1jancoolingdegreedays = "", since1jancoolingdegreedaysnormal = ""),   .Names = c("date", 
    "fog", "rain", "snow", "snowfallm", "snowfalli", "monthtodatesnowfallm", 
    "monthtodatesnowfalli", "since1julsnowfallm", "since1julsnowfalli", 
    "snowdepthm", "snowdepthi", "hail", "thunder", "tornado", "meantempm", 
    "meantempi", "meandewptm", "meandewpti", "meanpressurem", "meanpressurei", 
    "meanwindspdm", "meanwindspdi", "meanwdire", "meanwdird", "meanvism", 
    "meanvisi", "humidity", "maxtempm", "maxtempi", "mintempm", "mintempi", 
    "maxhumidity", "minhumidity", "maxdewptm", "maxdewpti", "mindewptm", 
    "mindewpti", "maxpressurem", "maxpressurei", "minpressurem", 
    "minpressurei", "maxwspdm", "maxwspdi", "minwspdm", "minwspdi", 
    "maxvism", "maxvisi", "minvism", "minvisi", "gdegreedays",      "heatingdegreedays", 
    "coolingdegreedays", "precipm", "precipi", "precipsource",    "heatingdegreedaysnormal", 
    "monthtodateheatingdegreedays", "monthtodateheatingdegreedaysnormal", 
    "since1sepheatingdegreedays", "since1sepheatingdegreedaysnormal", 
    "since1julheatingdegreedays", "since1julheatingdegreedaysnormal", 
    "coolingdegreedaysnormal", "monthtodatecoolingdegreedays",     "monthtodatecoolingdegreedaysnormal", 
    "since1sepcoolingdegreedays", "since1sepcoolingdegreedaysnormal", 
    "since1jancoolingdegreedays", "since1jancoolingdegreedaysnormal"
    ), class = "data.frame", row.names = 1L)

我正在使用以下命令从中检索数据

df <- data.frame()
df <- rbind(df, ldply(res, function(x) x[[1]]))

为了使用这个数据框,我使用 dt <- data.table(df) 将其转换为数据 table,现在我知道如何使用数据,例如 dt[.id=="fog"].

还有更elegant/efficient的解决方案吗?

@antoine-sac 解决了这个问题。没必要用apply获取数据,只是"un-nest"数据的问题。

您的问题是您的数据是 data.frame,其中一列是 date。但是 date 是一个 data.frame。正如您所说,它是一个嵌套列表。那么让我们"un-nest"吧。

你可以简单地做(假设你的数据在data):

df.date <- data$date
# removing incorrectly formated date from data
data$date <- NULL

此时数据是正常的data.frame,df.date也是基本的data.frame。

> df.date
                                    pretty year mon mday hour min          tzname
1 12:00 PM CDT on August 14,          2015 2015  08   14   12  00 America/Chicago

如果您想将其与现有 data.frame 合并:

# binding df.date with your data
data <- cbind(data, df.date)

无需任何申请。

现在,如果您不知道如何访问 data.frame 中的变量,那就是另一回事了。

如果你想,比如说,meantempm,你可以简单地做 data$meantempm

我推荐你参考关于 R 的初学者教程,有很多可供选择的 google 请求。