How to change the name of a column

Asked

Viewed 22,504 times

7

I have a date.frame, I changed the data class of the date column and later I separated the other three column through the Separate command, whose names became %Y, %m and %d, I would like to change these names to year, month and day. However, when trying to rename columns, using the Rename command gives error.

> as.data <- as.Date(dados$data)

> meus.dados <- separate(data = dados, col = data, into = c("%Y", "%m","%d"), sep = "-")

> meus.dados[1:5, 1:9]

Source: local data frame [5 x 9]

   posicao    %Y    %m    %d              especie    BF   CA2    CB    CP
     (chr) (chr) (chr) (chr)                (chr) (dbl) (dbl) (dbl) (dbl)
1  direita  2012    05    22     Canis familiaris     0     0     0     0
2  direita  2012    05    22     Canis familiaris     0     0     0     0
3 esquerda  2012    05    24 Dasypus novemcinctus     0     0     0     1
4 esquerda  2012    05    25     Canis familiaris     1     0     0     0
5 esquerda  2012    05    26 Dasypus novemcinctus     0     0     0     1

> rename(meus.dados, ano=" %Y", mes="%m", dia="%d")
 Error: Arguments to rename must be unquoted variable names. Arguments ano, mes, dia are not.

I don’t know what to do!!

  • 3

    Why not already create with the definitive names in the function separate? meus.dados <- separate(data = dados, col = data, into = c("ano", "mes","dia"), sep = "-")

  • Thank you!!! It worked!!!!

3 answers

8

You can use the function names to obtain, and also to change the name of the columns. In your case, you can use names(meus.dados)[2:4] <- c("ano", "mes", "dia"). The complete example below shows it being used:

meus.dados <- data.frame(
    posicao = c("direita", "direita", "esquerda", "esquerda"),
    YY = c("2012", "2012", "2012", "2012"),
    MM = rep("05", 4),
    DD = c("22", "22", "24", "25"),
    especie = c("cf", "cf", "dn", "cf")
)

names(meus.dados)[2:4] <- c("ano", "mes", "dia")

meus.dados

4

Since variable names have carcter not recognized directly by R, you need to use the character: ` wrapped in their name. Then it would be necessary to do so:

meus.dados <- data_frame(
  posicao = c("direita", "direita", "esquerda", "esquerda"),
  "%Y" = c("2012", "2012", "2012", "2012"),
  "%m" = rep("05", 4),
  "%d" = c("22", "22", "24", "25"),
  especie = c("cf", "cf", "dn", "cf")
)

rename(meus.dados, ano = `%Y`, mes = `%m`, dia = `%d`)

But as @molx said in the comment you could already put these names directly by the function separate.

  • For large banks it is advisable to use the function Rename.vars(data, c("x","y"), c("new_x","new_y")).

0

We can use the function rename_with of dplyr, which makes use of functions (like those of the package stringr) to search for patterns and perform name substitution.

Dice:

df_1 <- data.frame(
  posicao = c("direita", "direita", "esquerda", "esquerda"),
  "%Y" = c("2012", "2012", "2012", "2012"),
  "%m" = rep("05", 4),
  "%d" = c("22", "22", "24", "25"),
  especie = c("cf", "cf", "dn", "cf")
)

Function:

library(tidyverse)

df_1 %>% 
  rename_with(.data = ., .cols = 2:4, 
          .fn = str_replace, pattern = ".*", 
          replacement = str_c(c("ano", "mes", "dia")))


#   posicao  ano mes dia especie
#1  direita 2012  05  22      cf
#2  direita 2012  05  22      cf
#3 esquerda 2012  05  24      dn
#4 esquerda 2012  05  25      cf

Browser other questions tagged

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