I have a dataset that looks like this:
DT <- data.frame( rnorm(5),
rnorm(5),
rnorm(5),
rnorm(5),
rnorm(5),
rnorm(5))
names(DT) = c('a1[1]','a1[2]','a1[3]','a2[1]','a2[2]','a2[3]')
str(DT)
I would like to create new columns like:
diffa1 = a1[1] - a2[1]
diffa2 = a1[2] - a2[2]
diffa3 = a1[3] - a2[3]
I am wondering if there is anyway to do it without having to manually mutate through the IDs in the brackets because I have a1[1] up to a1[100], a2[1] up to a2[100], etc. Thanks!
a1_1. And you can assign names indata.frame, no need to add them later:data.frame(a1_1 = rnorm(5). For adding columns look atdplyr::mutate.dplyr::renameis another friend for those issues. Basically, if you're dealing with data frames, it's worth getting to knowdplyr.