嵌套的data.frame

时间:2015-09-02 17:43:32

标签: r plyr

我有一个嵌套的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)将其转换为数据表,现在我知道如何处理数据,例如dt[.id=="fog"]

是否有更优雅/更有效的解决方案?

@ antoine-sac解决了这个问题。没有必要使用apply来获取数据,这只是一个“取消嵌套”数据的问题。

1 个答案:

答案 0 :(得分:0)

您的问题是您的数据是data.frame,其列之一是date。但是date是一个data.frame。正如你所说它是一个嵌套列表。所以,让我们“窝”#34;它

您可以这样做(假设您的数据位于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的初学者教程,有很多可供选择的谷歌请求。