I have data frame with the first column as a categorical identifier, the second column as a frequency value and the remaining columns as raw data counts. I want to multiply all the count columns by the frequency column but not the first two.
All the raw count columns start with a capital letter followed by a full stop, e.g "L.abd", T.xyz etc.
For example, if I use the code:
require(dplyr)
ID <- c(1,2,3,4,5,6)
Freq <- c(0.1,0.2,0.3,0.5,0.1,0.3)
L.abc <- c(1,1,1,3,1,0)
L.ABC <- c(0,3,2,4,1,1)
T.xyz <- c(1,1,1,1,0,1)
F.ABC <- c(4,5,6,5,3,1)
df <- as.data.frame(cbind(ID, Freq, L.abc, L.ABC, T.xyz, F.ABC))
df_new <- df %>% mutate_each(funs(.*Freq), starts_with("L."))
I can create a new data frame containing the categorical data columns along with those columns starting with "L." which have been multiplied by the corresponding frequency value.
Is there a way to change the "starts_with" command to select all columns that begin with a capital letter and a full stop? My attempts to date using modifications such as "[A-Z]." have been unsuccessful.
Thanks in advance