Naming and renaming in R

Hi peeps!

I have just started learning R, as part of my MSc and i am struggling a bit. so part of our practice exercise is the following question:
Use the names() command to rename the columns of Mymatr in the data frame.

When i write:
names(Mydat)[4:7] <- c("Mymatr1","Mymatr2","Mymatr3","Mymatr4")

and i keep getting an error. So i tried removing and adding names in hope that it will correct it, but no happy endings yet!

These are the messages i am receiving:

Mydat
Multof2 Language mylet X1 X2 Lessthan15
1 2 English A 1 21 TRUE
2 4 English Z 2 22 TRUE
3 6 English C 3 23 TRUE
4 8 English C 4 24 TRUE
5 10 English Q 5 25 TRUE
6 12 Greek V 1 26 TRUE
7 14 Greek C 2 27 TRUE
8 16 Greek V 3 28 FALSE
9 18 Greek W 4 29 FALSE
10 20 Greek A 5 30 FALSE
11 22 English P 1 31 FALSE
12 24 English A 2 32 FALSE
13 26 English E 3 33 FALSE
14 28 English R 4 34 FALSE
15 30 English V 5 35 FALSE
16 32 Greek J 1 36 FALSE
17 34 Greek Q 2 37 FALSE
18 36 Greek I 3 38 FALSE
19 38 Greek W 4 39 FALSE
20 40 Greek V 5 40 FALSE

names(Mydat)[4:7] <- c("Mymatr1","Mymatr2","Mymatr3","Mymatr4","Mymatr5")
Error in names(Mydat)[4:7] <- c("Mymatr1", "Mymatr2", "Mymatr3", "Mymatr4", :
'names' attribute [7] must be the same length as the vector [6]
In addition: Warning message:
In names(Mydat)[4:7] <- c("Mymatr1", "Mymatr2", "Mymatr3", "Mymatr4", :
number of items to replace is not a multiple of replacement length

names(Mydat)[4:7] <- c("Mymatr1","Mymatr2","Mymatr3","Mymatr4","Mymatr5","Mymatr6","Mymatr7")
Error in names(Mydat)[4:7] <- c("Mymatr1", "Mymatr2", "Mymatr3", "Mymatr4", :
'names' attribute [7] must be the same length as the vector [6]
In addition: Warning message:
In names(Mydat)[4:7] <- c("Mymatr1", "Mymatr2", "Mymatr3", "Mymatr4", :
number of items to replace is not a multiple of replacement length
Mydat
Multof2 Language mylet X1 X2 Lessthan15
1 2 English A 1 21 TRUE
2 4 English Z 2 22 TRUE
3 6 English C 3 23 TRUE
4 8 English C 4 24 TRUE
5 10 English Q 5 25 TRUE
6 12 Greek V 1 26 TRUE
7 14 Greek C 2 27 TRUE
8 16 Greek V 3 28 FALSE
9 18 Greek W 4 29 FALSE
10 20 Greek A 5 30 FALSE
11 22 English P 1 31 FALSE
12 24 English A 2 32 FALSE
13 26 English E 3 33 FALSE
14 28 English R 4 34 FALSE
15 30 English V 5 35 FALSE
16 32 Greek J 1 36 FALSE
17 34 Greek Q 2 37 FALSE
18 36 Greek I 3 38 FALSE
19 38 Greek W 4 39 FALSE
20 40 Greek V 5 40 FALSE
names(Mydat)
[1] "Multof2" "Language" "mylet" "X1" "X2" "Lessthan15"
names
function (x) .Primitive("names")

names(Mydat)[4:7] <- c("Mymatr1","Mymatr2","Mymatr3","Mymatr4","Mymatr5","Mymatr6","Mymatr7")
Error in names(Mydat)[4:7] <- c("Mymatr1", "Mymatr2", "Mymatr3", "Mymatr4", :
'names' attribute [7] must be the same length as the vector [6]
In addition: Warning message:
In names(Mydat)[4:7] <- c("Mymatr1", "Mymatr2", "Mymatr3", "Mymatr4", :
number of items to replace is not a multiple of replacement length

names(Mydat)[4:7] <- c("Mymatr1","Mymatr2","Mymatr3","Mymatr4",)
Error in c("Mymatr1", "Mymatr2", "Mymatr3", "Mymatr4", ) :
argument 5 is empty

Please, enlighten me with what i am doing wrong!

Thnx

How many names are returned for your Mydat object? If you run length(names(Mydat) I suspect it will return 6 although there appears to be 7 columns.

Anyway, looking at the error message 'names' attribute [7] must be the same length as the vector [6] the problem might be solved by ensuring that the index is fitting with the actual number of columnnames, e.g. names(Mydat)[3:6] <- c("Mymatr1", "Mymatr2", "Mymatr3", "Mymatr4")

As a follow-up to lars,

what you are seeing on the screen is the row.number --leftmost column--and your 6 variables. If you manually count the variables displayed you will get 6 which is what

length(names(Mydat) 

will give you.