Modify data columns in R

Asked

Viewed 26 times

-1

I have an Excel spreadsheet with inflation expectations for 2021, 2022, 2023, 2024, 2025 over the last 30 days. When I import this spreadsheet, the dates are being considered as a column, and actually the variables should only be the years. I would need to have a data frame with 5 variables and 19 observations and not 6 variables with 19 observations. How do I make R understand that dates are not a variable?

This is the face of my data :

dados

To import, I used this code:

expec_anual_ipca <- read_excel("expec_anual_ipca.xls")
  • It would be nice if you could share a sample of your data with ``dput(head(df))`, where df is your data.frame.

  • You could use a command as much as select to remove this column or even specify it in the import command you are using as an argument

  • Hello Vinicius, thanks for the comment! But I do not want to remove, I just want the column of dates not read as a variable....

1 answer

2

If you install the package cellranger can specify the columns to read.

colTypes <- rep("numeric", 5)
cols <- cellranger::cell_cols(2:6)
expec_anual_ipca <- read_excel("expec_anual_ipca.xls", 
                               col_types = colTypes, 
                               range = cols)

Or, more simply,

expec_anual_ipca <- read_excel("expec_anual_ipca.xls", 
                               col_types = rep("numeric", 5), 
                               range = cellranger::cell_cols(2:6))

You can also specify by column names. In this case I assume that the second column is the column "B", the 3rd is the "C", etc, up to the column "F".

expec_anual_ipca <- read_excel("expec_anual_ipca.xls", 
                               col_types = rep("numeric", 5), 
                               range = cellranger::cell_cols("B:F"))
  • Thank you very much Rui!!! I will try here, installing this package that I did not know. Thanks even!

Browser other questions tagged

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