[R] Constructing a data.frame from csv files
jim holtman
jholtman at gmail.com
Wed Jan 11 14:49:44 CET 2012
The error message says it all: the dataframes that you are creating,
and then trying to 'rbind', do not have the same columns. You need to
at least show what the first couple of lines of each of you input
files are, or output the names of the columns as you are reading the
files. This is some elementary debugging that you will have to learn.
On Wed, Jan 11, 2012 at 7:38 AM, Vincy Pyne <vincy_pyne at yahoo.ca> wrote:
> Dear R helpers,
>
> Following is my R code where I am trying to calculate returns and then trying to create a data.frame. Since, I am not aware how many instruments I will be dealing so I have constructed a function. My R code is as follows -
>
> library(plyr)
>
> mydata <- data.frame(instru_name = c("instru_A","instru_A","instru_A","instru_A","instru_A","instru_A","instru_A","instru_A","instru_A","instru_A","instru_A","instru_A","instru_A","instru_A","instru_B","instru_B","instru_B","instru_B","instru_B","instru_B","instru_B","instru_B","instru_B","instru_B","instru_B","instru_B","instru_B","instru_B"), date = c("10-Jan-12","9-Jan-12","8-Jan-12", "7-Jan-12", "6-Jan-12","5-Jan-12","4-Jan-12","3-Jan-12","2-Jan-12","1-Jan-12", "31-Dec-11", "30-Dec-11","29-Dec-11","28-Dec-11","10-Jan-12","9-Jan-12","8-Jan-12", "7-Jan-12","6-Jan-12","5-Jan-12","4-Jan-12","3-Jan-12","2-Jan-12","1-Jan-12","31-Dec-11","30-Dec-11","29-Dec-11","28-Dec-11"), price = c(11.9,10.5,13,14.5,14.4,14.8,10.1,12,14.3, 10.7,11.2,10.2,10.2,10.8,41.9,40.5,43,44.5,44.4,48.8,42.1,44,46.3,48.7,46.2,44.2,42.2,40.8))
>
> attach(mydata)
>
> opt_return_volatilty = function(price, instru_name)
>
> {
>
> price_returns = matrix(data = NA, nrow = (length(price)-1), ncol = 1)
> for (i in(1:(length(price)-1)))
> {
> price_returns[i] = log(price[i]/price[i+1])
> }
> volatility = sd(price_returns)
> entity_returns = unique(instru_name)
> colnames(price_returns) = entity_returns
>
> write.csv(price_returns, file = paste(entity_returns, ".csv", sep = ""), row.names = FALSE)
>
> return(data.frame(list(volatility = volatility)))
>
> }
>
> entity_volatility <- ddply(.data=mydata, .variables = "instru_name",
> .fun=function(x) opt_return_volatilty(price = x$price, instru_name = x$instru_name))
>
>
>> entity_volatility
> instru_name volatility
> 1 instru_A 0.17746897
> 2 instru_B 0.06565341
>
>
> fileNames <- list.files(pattern = "instru.*.csv")
>
>> fileNames
> [1] "instru_A.csv" "instru_B.csv"
>
> # _____________________________________________________________________________________
>
> # MY QUERY
>
> # I need to construct the data frame consisting of all the returns. I.e. I need to have # a data.frame like
>
> instru_A instru_B
> 0.125163143 0.033983853
> -0.2135741 -0.059898142
> -0.109199292 -0.034289073
> 0.006920443 0.00224972
> -0.027398974 -0.094490843
>
> I am using following Code
>
> input <- do.call(rbind, lapply(fileNames, function(.name)
> {
> .data <- read.csv(.name, header = TRUE, as.is = TRUE)
> .data$file <- .name
> .data
> }))
>
> # I get following error.
>
> Error in match.names(clabs, names(xi)) :
> names do not match previous names
>
>
> Kindly guide
>
> Regards
>
> Vincy
>
>
>
> [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Jim Holtman
Data Munger Guru
What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.
More information about the R-help
mailing list