guRus I have say a dataframe, d and I wish to do the following: 1) For each row, I want to take one particular value of the row and multiply it by 2. How do I do it. Say the data frame is as below: OPEN HIGH LOW CLOSE 1931.2 1931.2 1931.2 1931.2 0 0 0 999.05 0 0 0 1052.5 0 0 0 987.8 0 0 0 925.6 0 0 0 866 0 0 0 1400.2 0 0 0 754.5 0 0 0 702.6 0 0 0 653.25 0 0 0 348 0 0 0 801 866.55 866.55 866.55 866.55 783.1 783.1 742.25 742.25 575 575 575 575 0 0 0 493 470 470 420 425 355 360 343 360 312.05 312.05 274 280.85 257.35 257.35 197 198.75 182 185.95 137 150.75 120.25 129 90.7 101.25 91.85 91.85 57 66.6 How do I multiply only the close of every row using the 'apply' function? And once multiplied how do I obtain a new table that also contains the new 2*CLOSE column (without cbind?). 2) Also, how do I run a generic function per row. Say for example I want to calculate the Implied Volatility for each row of this data frame ( using the RMterics package). How do I do that please using the apply function? I am focusing on apply because I like the vectorisation concept in R and I do not want to use a for loop etc. Many thanks for the enlightment, Raghu [[alternative HTML version deleted]]
For 1, an easy way is dat <- transform(dat, CLOSE2=2*CLOSE) For 2: apply(dat,1,fun) On 08/06/2010 03:06 PM, Raghuraman Ramachandran wrote:> guRus > > I have say a dataframe, d and I wish to do the following: > > 1) For each row, I want to take one particular value of the row and multiply > it by 2. How do I do it. Say the data frame is as below: > OPEN HIGH LOW CLOSE 1931.2 1931.2 1931.2 1931.2 0 0 0 999.05 0 0 0 1052.5 > 0 0 0 987.8 0 0 0 925.6 0 0 0 866 0 0 0 1400.2 0 0 0 754.5 0 0 0 702.6 0 0 0 > 653.25 0 0 0 348 0 0 0 801 866.55 866.55 866.55 866.55 783.1 783.1 742.25 > 742.25 575 575 575 575 0 0 0 493 470 470 420 425 355 360 343 360 312.05 > 312.05 274 280.85 257.35 257.35 197 198.75 182 185.95 137 150.75 120.25 129 > 90.7 101.25 91.85 91.85 57 66.6 > > How do I multiply only the close of every row using the 'apply' function? > And once multiplied how do I obtain a new table that also contains the new > 2*CLOSE column (without cbind?). > > 2) Also, how do I run a generic function per row. Say for example I want to > calculate the Implied Volatility for each row of this data frame ( using the > RMterics package). How do I do that please using the apply function? I am > focusing on apply because I like the vectorisation concept in R and I do not > want to use a for loop etc. > > Many thanks for the enlightment, > Raghu > > [[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. >-- Abhijit Dasgupta, PhD Director and Principal Statistician ARAASTAT Ph: 301.385.3067 E: adasgupta at araastat.com W: http://www.araastat.com
Raghuraman Ramachandran wrote:> guRus > > I have say a dataframe, d and I wish to do the following: > > 1) For each row, I want to take one particular value of the row and multiply > it by 2. How do I do it. Say the data frame is as below: > OPEN HIGH LOW CLOSE 1931.2 1931.2 1931.2 1931.2 0 0 0 999.05 0 0 0 1052.5 > 0 0 0 987.8 0 0 0 925.6 0 0 0 866 0 0 0 1400.2 0 0 0 754.5 0 0 0 702.6 0 0 0 > 653.25 0 0 0 348 0 0 0 801 866.55 866.55 866.55 866.55 783.1 783.1 742.25 > 742.25 575 575 575 575 0 0 0 493 470 470 420 425 355 360 343 360 312.05 > 312.05 274 280.85 257.35 257.35 197 198.75 182 185.95 137 150.75 120.25 129 > 90.7 101.25 91.85 91.85 57 66.6Much easier to give us the ?dput output so we can construct this!> > How do I multiply only the close of every row using the 'apply' function? > And once multiplied how do I obtain a new table that also contains the new > 2*CLOSE column (without cbind?).Guessing here... but why do you want to use apply? Isn't df$new.col <- df$CLOSE * 2 what you want?> > 2) Also, how do I run a generic function per row. Say for example I want to > calculate the Implied Volatility for each row of this data frame ( using the > RMterics package). How do I do that please using the apply function? I am > focusing on apply because I like the vectorisation concept in R and I do not > want to use a for loop etc.Simply read ?apply to see examples. Apply works on matrices, not data.frames, so if you give it a data.frame, it will be coerced into a matrix. apply(your.data, 1, your.function)
> How do I multiply only the close of every row using the 'apply' function? > And once multiplied how do I obtain a new table that also contains the new > 2*CLOSE column (without cbind?).You don't use apply in this case - a simple multiplication and variable assignment will do:> require(tseries) > foo <- get.hist.quote('^GDAXI') > foo[1:10, ]Open High Low Close 1991-01-02 1375.4 1375.4 1359.1 1366.1 1991-01-03 1371.7 1374.7 1365.2 1366.7 1991-01-04 1375.4 1398.0 1375.4 1396.1 1991-01-07 1373.6 1373.6 1352.5 1358.2 1991-01-08 1350.4 1357.1 1345.5 1354.0 1991-01-09 1358.6 1380.8 1358.0 1375.2 1991-01-10 1367.9 1383.7 1363.1 1383.4 1991-01-11 1401.3 1406.4 1376.9 1382.3 1991-01-14 1354.5 1354.5 1327.8 1327.8 1991-01-15 1327.0 1330.3 1312.4 1325.6> foo$Close <- foo$Close * 2 > foo$Close <- foo$Close * 2 > foo[1:10, ]Open High Low Close 1991-01-02 1375.4 1375.4 1359.1 2732.2 1991-01-03 1371.7 1374.7 1365.2 2733.4 1991-01-04 1375.4 1398.0 1375.4 2792.2 1991-01-07 1373.6 1373.6 1352.5 2716.4 1991-01-08 1350.4 1357.1 1345.5 2708.0 1991-01-09 1358.6 1380.8 1358.0 2750.4 1991-01-10 1367.9 1383.7 1363.1 2766.8 1991-01-11 1401.3 1406.4 1376.9 2764.6 1991-01-14 1354.5 1354.5 1327.8 2655.6 1991-01-15 1327.0 1330.3 1312.4 2651.2> 2) Also, how do I run a generic function per row. Say for example I want to > calculate the Implied Volatility for each row of this data frame ( using the > RMterics package). How do I do that please using the apply function? I am > focusing on apply because I like the vectorisation concept in R and I do not > want to use a for loop etc.You can get the manual page of any R-command by either preceding it by a question mark or giving the command as an argument to the help function - specificly: ?apply help(apply) Especially the example section is useful for a jumpstart. Here is an example of computing row means: apply(foo, 1, mean) Instead of 'mean' you can insert whatever function you'd like to apply. cu Philipp -- Dr. Philipp Pagel Lehrstuhl f?r Genomorientierte Bioinformatik Technische Universit?t M?nchen Wissenschaftszentrum Weihenstephan Maximus-von-Imhof-Forum 3 85354 Freising, Germany http://webclu.bio.wzw.tum.de/~pagel/
Reasonably Related Threads
- Problem Reading from .txt
- the same function returning different values when called differently..
- Fwd: the same function returning different values when called differently..
- Question about error of "non-numeric argument to binary operator"
- Accounting for overdispersion in a mixed-effect model with a proportion response variable and categorical explanatory variables.