So I have created a data.frame that has n columns like so:
df <- as.data.frame(matrix(nrow = 3, ncol = 15))
It looks like this:
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15
1 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
2 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
3 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
I want change every Nth column name, so it looks like this:
A V2 V3 B V5 V6 C V8 V9 D V11 V12 E V14 V15
1 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
2 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
3 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
I have made this sequence, that gives me colnames I want:
colnames(df[,seq(1,ncol(df),3)])
The output:
[1] "V1" "V4" "V7" "V10" "V13"
But when I try to change names nothing happens:
colnames(df[,seq(1,ncol(df),3)]) <- c("A","B","C","D","E")
I also tried this, but it only changes the first column and it gives warning message:
names <- colnames(df[,seq(1,ncol(df),3)])
colnames(df)[colnames(df) == names] <- c("A","B","C","D","E")
The warning message:
Warning message:
In colnames(df)[colnames(df) == names] <- c("A", "B", "C", "D", :
number of items to replace is not a multiple of replacement length
Thank you in advance for your help, I appreciate every answer :)