简体   繁体   中英

How do I convert a list of zoo objects into a dataframe

I have a list containing zoo objects that I would like to convert into a dataframe in long format.

My current output looks like this:

current_output <- list(structure(c(3.596, 3.547, 3.375, 3.235, 3.655, 3.185), 
               SiteName = "Well...222 Comminutor Stn", 
               Measurement = "Depth From Measuring Point", 
               Units = "m", InterpolationMethod = "Quasi-continuous", 
               DataType = "SimpleTimeSeries", 
               TSType = "StdSeries", 
               class = "zoo", index = structure(c(935494200L, 937990800L, 940318200L, 942745500L, 945245700L, 947493000L), 
                                                class = c("POSIXct", "POSIXt"), tzone = "UTC")), 
     structure(c(-5.76, -5.54, -5.19, -1.67, -2.37, -2.18, -5.81), 
               SiteName = "Well..1003 Bridge Pa", 
               Measurement = "Depth From Measuring Point", 
               Units = "m", InterpolationMethod = "Quasi-continuous", 
               DataType = "SimpleTimeSeries", TSType = "StdSeries", 
               class = "zoo", index = structure(c(1240493880L,1242117900L, 1245239100L, 1247761800L, 1250082600L, 1253016000L,947493000L), 
                                                class = c("POSIXct", "POSIXt"), tzone = "UTC")))

I want it do look like this?

   Index                 zoodata    Site                        Measurement            
1  1999-08-24 11:30:00   3.596      Well...222 Comminutor Stn   Depth From Measuring Point [Manual Water Level]
2  1999-09-22 09:00:00   3.547      Well...222 Comminutor Stn   Depth From Measuring Point [Manual Water Level]
3  1999-10-19 07:30:00   3.375      Well...222 Comminutor Stn   Depth From Measuring Point [Manual Water Level]
4  1999-11-16 09:45:00   3.235      Well...222 Comminutor Stn   Depth From Measuring Point [Manual Water Level]
5  1999-12-15 08:15:00   3.655      Well...222 Comminutor Stn   Depth From Measuring Point [Manual Water Level]
6  2000-01-10 08:30:00   3.185      Well...222 Comminutor Stn   Depth From Measuring Point [Manual Water Level]
7  2009-04-23 13:38:00  -5.760      Well..1003 Bridge Pa        Depth From Measuring Point [Manual Water Level]
8  2009-05-12 08:45:00  -5.540      Well..1003 Bridge Pa        Depth From Measuring Point [Manual Water Level]
9  2009-06-17 11:45:00  -5.190      Well..1003 Bridge Pa        Depth From Measuring Point [Manual Water Level]
10 2009-07-16 16:30:00  -1.670      Well..1003 Bridge Pa        Depth From Measuring Point [Manual Water Level]
11 2009-08-12 13:10:00  -2.370      Well..1003 Bridge Pa        Depth From Measuring Point [Manual Water Level]
12 2009-09-15 12:00:00  -2.180      Well..1003 Bridge Pa        Depth From Measuring Point [Manual Water Level]
13 2000-01-10 08:30:00  -5.810      Well..1003 Bridge Pa        Depth From Measuring Point [Manual Water Level]

You can convert the zoo object to dataframe using fortify.zoo and get the remaining column from the attributes of each object.

do.call(rbind, lapply(current_output,function(x) cbind(zoo::fortify.zoo(x), 
        Sitename = attr(x, 'SiteName'), Measurement = attr(x, 'Measurement'))))


#                 Index      x                  Sitename                Measurement
#1  1999-08-24 11:30:00  3.596 Well...222 Comminutor Stn Depth From Measuring Point
#2  1999-09-22 09:00:00  3.547 Well...222 Comminutor Stn Depth From Measuring Point
#3  1999-10-19 07:30:00  3.375 Well...222 Comminutor Stn Depth From Measuring Point
#4  1999-11-16 09:45:00  3.235 Well...222 Comminutor Stn Depth From Measuring Point
#5  1999-12-15 08:15:00  3.655 Well...222 Comminutor Stn Depth From Measuring Point
#6  2000-01-10 08:30:00  3.185 Well...222 Comminutor Stn Depth From Measuring Point
#7  2009-04-23 13:38:00 -5.760      Well..1003 Bridge Pa Depth From Measuring Point
#8  2009-05-12 08:45:00 -5.540      Well..1003 Bridge Pa Depth From Measuring Point
#9  2009-06-17 11:45:00 -5.190      Well..1003 Bridge Pa Depth From Measuring Point
#10 2009-07-16 16:30:00 -1.670      Well..1003 Bridge Pa Depth From Measuring Point
#11 2009-08-12 13:10:00 -2.370      Well..1003 Bridge Pa Depth From Measuring Point
#12 2009-09-15 12:00:00 -2.180      Well..1003 Bridge Pa Depth From Measuring Point
#13 2000-01-10 08:30:00 -5.810      Well..1003 Bridge Pa Depth From Measuring Point

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM