[R] Rearranging Data
Jia Ying Mei
jiamei at Princeton.EDU
Thu Jul 31 06:12:46 CEST 2008
Hi everyone,
A few days back Jim helped with out, helping me find an automated way of
determining the mean and median of intervals between price changes.
However, a new set of data I have is no longer in the same format.
CDSmod is the file that was used with this code:
x <- read.csv('/cdsmod.csv', as.is=TRUE, check.names=FALSE)
x$Date <- as.Date(x$Date, "%m/%d/%Y")
require(zoo)
out <- lapply(x[-1], function(.cnty){ # iterate for each country
# create a dataframe of the date and currency
.df <- data.frame(date=x[[1]], cur=.cnty)
.df <- .df[order(.df$date),] # sort in date order
.df$cur <- na.locf(.df$cur, na.rm=FALSE) # carry forward missing values
.df <- .df[complete.cases(.df),] # delete NAs at the beginning
# mark the breaks
.df$grp <- cumsum(c(TRUE, head(.df$cur, -1) != tail(.df$cur, -1)))
# now split out each group and calculate its length
# if it is not the last group, then the end of the span if one day earlier
# than the next entry. Working with the indices (row numbers)
into the dataframe
# to make this easier
.len <- sapply(split(seq(nrow(.df)), .df$grp), function(.indx){
if (tail(.indx, 1) != nrow(.df)){ # not the last split
# subtract 1 day from start of next range
.df$date[tail(.indx, 1) + 1] - 1 - .df$date[.indx[1]]
}
else {
.df$date[tail(.indx, 1)] - .df$date[.indx[1]]
}
})
})
# output the mean and median
t(sapply(out, function(.cntry) c(number=length(.cntry),
mean=mean(.cntry), median=median(.cntry))))
I wanted to know if there is a way to rearrange data from IKEALASTmod so
that we can use something like the code above. The new problem is that
instead of date and country only, there is now date, country and
product. So is it possible to rearrange my data to have the output from
above be by country and price.
Hope I was clear, thanks for any help you can offer!
Jia Ying Mei
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: CDSmod.txt
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20080731/5a3b4104/attachment.txt>
More information about the R-help
mailing list