I have the oddest problem, it's never happened to me before and I haven't found any solution yet.
I'm working on a dataframe where several columns have similar names (S1Q1.pre / S1Q1.post, S1Q2.pre/S1Q2.post, ..., S1Qx.pre/S1Qx.post etc).
Example:
delta <- data.frame(S1Q1.pre = sample(1:7, 5, replace=TRUE), S1Q1.post = sample(1:7, 5, replace=TRUE), S1Q2.pre = sample(1:7, 5, replace=TRUE), S1Q2.post = sample(1:7, 5, replace=TRUE),
S1Q3.pre = sample(1:7, 5, replace=TRUE), S1Q3.post = sample(1:7, 5, replace=TRUE),S1Q4.pre = sample(1:7, 5, replace=TRUE), S1Q4.post = sample(1:7, 5, replace=TRUE))
For each of these pairs of variables (.pre/.post), I want to calculate a difference as follows:
S1Qx.delta = S1Qx.post - S1Qx.pre
I have a problem with the name of the new variable S1Qx.delta.
I've used this loop:
S1list <- paste0('S1Q',1:4)
for (q in 1:length(S1list)) {
quest <- S1list[q]
delta$temp <- NA
delta$temp <- delta[which(colnames(delta) == paste0(quest,'.post'))] -
delta[which(colnames(delta) == paste0(quest,'.pre'))]
names(delta)[which(names(delta) == 'temp')] <- paste0(quest,'.delta')
}
It seems to work almost perfectly: I obtain the new variables S1Qx.delta as I wanted. I can retrieve this variables with delta$S1Qx.delta as usual.
But when I use names(), colnames() or head(), I get this:
names(delta$S1Q1.delta)
[1] "S1Q1.post"
Has anyone ever had this problem? Do you know how to solve it?
Note: I've tried the same loop with different syntaxes to rename delta$temp but it doesn't solve my problem. For instance I've tried:
colnames(delta)[which(colnames(delta) == 'temp')] <- paste0(quest,'.delta')
names(delta)[names(delta) == 'temp'] <- paste0(quest,'.delta')
The issue is that you when computing the differences you extract the columns using
[(instead of[[). As a result you don't get vectors but 1-columndata.frames instead. Hence yourtempor.deltacolumns are 1-columndata.frames too:Instead, you could use
[[to get vectors:But overall you could achieve your desired result more easily like so: