1

I have the following data set:

    Date                 sector Forward TRT1Y USD CIQ MKVAL
  (time)                 (fctr)             (dbl)     (dbl)
1 1999-09-30 Consumer Discretionary        0.11622590 19856.182
2 1999-09-30            Industrials       -0.13436770     0.000
3 1999-09-30            Industrials        0.09085045     0.000
4 1999-09-30             Financials        0.69653463  6142.963
5 1999-09-30 Consumer Discretionary        0.00000000     0.000
6 1999-09-30             Financials        1.61538458  1510.159
7 1999-09-30             Financials        0.27569774     0.000
8 1999-09-30            Industrials       -0.33099464     0.000
9 1999-09-30 Consumer Discretionary        0.38051024 61308.492
10 1999-09-30           Industrials        0.07535502     0.000

Dates are quarterly and go up until 12/31/15. Also, here is the structure:

Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   97136 obs. of  4 variables:
 $ Date             : POSIXct, format: "1999-09-30" "1999-09-30" "1999-09-30" 
 $ sector           : Factor w/ 11 levels "Consumer Discretionary",..: 8 6 7 4 
 $ Forward TRT1Y USD: num  0.1162 -0.1344 0.0909 0.6965 0 ...
 $ CIQ MKVAL        : num  19856 0 0 6143 0 ...

I'm trying to create a weight column based on 'CIQ MKVAL' and would like it to do the weighting by date and by sector. The weight will then help me derive a weighted average forward TRT1Y for each sector and by each date. Here is the weight function:

weight <- function(x) x/sum(x)

This is the code I'm using to find the weighting and splitting the data frame by date and sector

ddply(wMean1, .(Date,sector), transform, w=weight('CIQ MKVAL'))

But, when I'm running the code, I get the following error:

Error in sum(x) : invalid 'type' (character) of argument

I have found something at this site: http://www.r-bloggers.com/type-conversion-and-you-or-and-r/ that I believe says that when doing the above calculation it's converting my numeric fields to characters. I am fairly new to R and I haven't figured out a way around this. Does anybody have any suggestions?

putty
  • 744
  • 1
  • 6
  • 14
  • Please share your data, output of `str()` is of not much use. – mtoto Mar 31 '16 at 17:15
  • The error message seems reasonably clear. 'CIQ MKVAL' is a character object and it makes no sense to either take a sum of a character value or divide it by some value. – IRTFM Mar 31 '16 at 17:53
  • In other words, you need to remove the quotes from around `'CIQ MKVAL'`. – aosmith Mar 31 '16 at 18:21
  • Wow. I guess I just assumed I was passing in a numeric column so it didn't matter what the column name was. Thanks for helping out a struggling beginner. I appreciate it. – putty Mar 31 '16 at 18:33

0 Answers0