I have the following dataframe and I am wondering how to subtract the value in the first row to values in the same column.
I would also be interested to know how to subtract any column or value to other rows.
Here are two methods. In the first the values are changed in the original column and in the second, which I prefer, a new column is made with the adjusted values.
Is there a way to also delete the row NULL directly or is it something that I can only do later, for example by selecting row 2 to 4 to create a new dataframe?
I tried with a more complex example, more representative of my data but I do not really understand why it does not work. It always makes result equal to zero.
Is it because I group my samples in order to determine the mean for each group?
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
Enzyme <- tibble(Sample = c("Blank", "Blank", "ProtA", "ProtA", "ProtB", "ProtB", "ProtC", "ProtC"),
`24H` = c(0.033, 0.035, 0.201, 0.188, 0.345, 0.321, 0.245, 0.222))
Enzyme %>%
group_by(Sample) %>%
summarize(Mean = mean(`24H`)) %>%
rename("24H" = "Mean") %>%
mutate(ActivityAdj = `24H` - first(x = `24H`))
#> # A tibble: 4 x 3
#> Sample `24H` ActivityAdj
#> <chr> <dbl> <dbl>
#> 1 Blank 0.034 0
#> 2 ProtA 0.194 0.160
#> 3 ProtB 0.333 0.299
#> 4 ProtC 0.233 0.199
As to why you failed, when you extract an element from a data.frame, it's a vector. But if you extract from a tibble, it's a tibble itself, of one variable and one observation. Subtraction of a tibble of suzh shape from a vector returns the result for first element of vector only, and then it gets replicated to match the length. That's why you are getting all zeros.
Now I think this is the reason just by checking a few combinations. These, especiallly extracting one element still being a tibble, seems odd to me. But there may be a documentation somewhere which I missed.