I am trying to exponent a certain part of the data set here.
I have a table like this: Called DF
Participant | Date | XPL | WAYS | SPM |
---|---|---|---|---|
Location | TBD | 12 | 3 | 5.5 |
George | 10_Sep_Mon | 23 | 12 | 12.7 |
Sam | 10_Sep_Mon | 14 | 2.5 | 5 |
Carl | 10_Sep_Mon | 21 | 2.8 | 5.1 |
I only want to apply an operation on the second row, starting on George, and just the section between XPL, WAYS and SPM. I do not want to do any calculation on the first row.
Participant | Date | XPL | WAYS | SPM |
---|---|---|---|---|
Location | TBD | 12 | 3 | 5.5 |
George | 10_Sep_Mon | 23 | 12 | 12.7 |
Sam | 10_Sep_Mon | 14 | 2.5 | 5 |
Carl | 10_Sep_Mon | 21 | 2.8 | 5.1 |
The end result, I want to put that number to the power of 3, so for example, 3^23 for George at XPL. What is the best way to do this for a large data set where it would be hard to "hard code" it?
Participant | Date | XPL | WAYS | SPM |
---|---|---|---|---|
Location | TBD | 12 | 3 | 5.5 |
George | 10_Sep_Mon | 3^23 | 3^12 | 3^12.7 |
Sam | 10_Sep_Mon | 3^14 | 3^2.5 | 3^5 |
Carl | 10_Sep_Mon | 3^21 | 3^2.8 | 3^5.1 |
So ideally, I would have the numerical value for the section applied. Any suggestions would be appreciated!
Participant <- c("Location", "George", "Sam", "Carl")
Date <- c("TBD", "10_Sep_Mon", "10_Sep_Mon", "10_Sep_Mon")
XPL <- c(12, 23, 14, 21)
WAYS <- c(3, 12, 2.5, 2.8)
SPM <- c(5.5, 12.7, 5, 5.1)
DF <- data.frame(Participant, Date, XPL, WAYS, SPM)
CodePudding user response:
Using dplyr:
library(dplyr)
DF %>% mutate(across(XPL:SPM, ~ c(.x[1], 3^.x[-1])))
#> Participant Date XPL WAYS SPM
#> 1 Location TBD 12 3.00000 5.5000
#> 2 George 10_Sep_Mon 94143178827 531441.00000 1146673.9198
#> 3 Sam 10_Sep_Mon 4782969 15.58846 243.0000
#> 4 Carl 10_Sep_Mon 10460353203 21.67402 271.2179
Created on 2022-09-28 with reprex v2.0.2
CodePudding user response:
Using base R
DF[-1, 3:5] <- 3^DF[-1, 3:5]
-output
> DF
Participant Date XPL WAYS SPM
1 Location TBD 12 3.00000 5.5000
2 George 10_Sep_Mon 94143178827 531441.00000 1146673.9198
3 Sam 10_Sep_Mon 4782969 15.58846 243.0000
4 Carl 10_Sep_Mon 10460353203 21.67402 271.2179