How to delete and then create all variable names in a data.frame in R?

Asked

Viewed 153 times

1

Let’s say I have a data.frame 6x5, for example:

print(Dados)

Linha  A   B   C   D   E
L1     4   3   2   2   4
L2     1   11  1   1   1
L3     0   1   2   3   4
L4     2   0   0   8   0

But I want to exclude the "head" of data.frame, i.e., exclude the names of all variables, example:

print(Dados_Sem_Cabeça)

    L1     4   3   2   2   4
    L2     1   11  1   1   1
    L3     0   1   2   3   4
    L4     2   0   0   8   0

And then I would like to create a "new head" with different variable names, example:

print(Dados_Cabeça_Nova)

    C1   C2   C3   C4   C5  C6
    L1   4    3    2    2   4
    L2   1    11   1    1   1
    L3   0    1    2    3   4
    L4   2    0    0    8   0

NOTE: I cannot select the names of the variables because when the data.frame undergo updates the names of the variables will change, example:

print(Dados_Atualizados)

    Linha  B   C   D   E   F
    L1     4   3   2   2   4
    L2     1   11  1   1   1
    L3     0   1   2   3   4
    L4     2   0   0   8   0

How can I delete and then create all variable names in one data.frame?

2 answers

2

You’ll have to pass NULL to the names of your data.frame

"Excluding":

names(iris)
[1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width"  "Species"     
names(iris) <- NULL
names(iris)
NULL
head(iris, 2)

1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa

Renaming:

names(iris) <- paste0("Nome", 1:5)
names(iris)
[1] "Nome1" "Nome2" "Nome3" "Nome4" "Nome5"
head(iris,2)
  Nome1 Nome2 Nome3 Nome4  Nome5
1   5.1   3.5   1.4   0.2 setosa
2   4.9   3.0   1.4   0.2 setosa
  • Thank you for the reply @Guilherme Parreira. But in my case I can’t use variable names because with each update of the data.frame they change. I edited the question by adding this little detail.

  • 1

    pq vc need to delete the actual names?

  • Pq I cannot select for example the variable "A", pq in the next update the variable changes. Example: Variáveis_Hoje = A, B, C, D, E and then changes to Variáveis_Amanhã: B,C,D,E,F. If I select by name "A" in the next update my script will give error, pq the variable "A" will no longer exist.

  • The idea of deleting the "head" of the data.frame is to make sure that my script does not depend on the names of the variables that always change.

  • 1

    um, got it, so it’s using the NULL same. because then you can’t call the column name directly.

  • How can I use the NULL? And how to add the line with other variable names? You can edit your answer by clarifying please?

  • but I edited my answer with the NULL

Show 3 more comments

1


Actually the solution is quite simple: you do not need to delete the names. Every time you update the data.frame just update also what are the columns.


EXAMPLE

I will create an example data.frame from an array

dados = as.data.frame(matrix(sample(x=1:10, size = 20, replace = T), nrow=4))

which may result in

   V1 V2 V3 V4 V5
1  5  6  1  2  6
2  9  6  9  4 10
3  1  9  1  6  5
4 10  6  9  8  6

Here you can update the names simply with the colnames()

colnames(dados) <- c('A', 'B', 'C', 'D', 'E')

and ready, your data.frame is now

   A B C D  E
1  5 6 1 2  6
2  9 6 9 4 10
3  1 9 1 6  5
4 10 6 9 8  6

OBSERVING

It’s my instinct based on the question you asked: I believe that because you’re in need of rewriting this data.frame probably you are addressing your problem in some way not optimized.

  • Thank you for the reply Flavio Barros. I reached the same conclusion of your reply tbm.

  • Even I was going to answer my own question with an answer close to yours, but you came first. Great solution!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.