简体   繁体   中英

R reorganizing the data frame

I am a newbie in R, and I think I need done is do-able with reshape, melt and cast functions. I did some reading but cannot figure out how to go about doing it.

I have a data frame like this:

)
server1, 01/01/2012, Memory, 27.200000, 27.490000, 28.560000, 29.300000
server1, 01/01/2012, disk , 2.330000, 22.380000, 7.580000, 2.440000
server1, 01/01/2012, CPU 0.470000, 36.500000, 12.230000, 0.350000
server1, 01/01/2012, interface 2.330000, 22.380000, 7.580000, 2.440000

I like to convert this data frame to something like this, so that R can easily read and graph this data:

server1, 01/01/2012:01:00, 27.200000, 2.330000,0.470000, 2.330000  
server1, 01/01/2012:02:00, 27.49, 22.38, 36.50, 22.38
server1, 01/01/2012:03:00, 28.56, 7.58, 12.23, 7.58
server1, 01/01/2012:04:00, 29.30, 2.44, 0.35, 2.44
  1. combining ExtractedDate with Hour1 (eg if the Heading says Hour1, the Exracted Date should be "01/01/2012 01:00"

  2. turn metrictype to header (header should be: put Hourly data in columns rather than rows.

can someone help me with this?

update=======

playing with melt and cast, I thing I am getting very close. I did these 3 commands:

 1. x <- data.frame(read.table("out.txt", sep=",", header=T))
 2. y <- melt(x, id=c("Hostname", "Date", "Met"))
 3. yy <- cast(y, Hostname + Date + variable ~ Mete)

my output looks like this:

     Hostname 

 1  server1  01/29/2012    Hour1       0.72       2.33     23.76
 2  server1  01/29/2012    Hour2       2.38       2.86     23.82
 3  server1  01/29/2012    Hour3       9.59       6.25     24.85
 4  server1  01/29/2012    Hour4      31.09      18.41     25.87
 5  server1  01/29/2012    Hour5       0.42       1.92     24.24
 6  server1  01/29/2012    Hour6       1.79       2.65     24.31

The problem I am facing now is that I have to combine Date and variable field. For example, 1st rows date field should be 01/29/2012 01:00, second row, 01/29/2012 02:00 so forth, if someone can help me with this, that would be awesome.

use something like gsub first:

yy$variable <- as.integer(gsub('Hour', '', yy$variable))

Then turn it into a POSIX time format:

yy$variable <- paste(yy$variable, ':00:00', sep='')

Convert your date to a date:

yy$Date <- as.Date(yy$Date, format='%m/%d/%Y')

Then mash it together:

yy$date_time <- as.POSIXct(paste(yy$Date, yy$variable))

I assume there is a pretty way using the zoo package, but I don't know it well at all.

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