Mateus Maciel
Mateus Maciel

Reputation: 161

Delete the last number from a row in R

I have the following dataset and I wish to delete the last number from each row:

id_municipio       
 1 5300108
 2 5300108
 3 5300108  

I would like to transform it into:

id_municipio       
 1 530010
 2 530010
 3 530010

All the rows have 7 numbers, and I want to delete the last one to transform it into a 6 number length.

Upvotes: 0

Views: 26

Answers (1)

ko3
ko3

Reputation: 1786

I am not sure which programming language you use and which type your column has. Assuming you use python and the column is an integer type, you can use this approach:

import pandas as pd
df = pd.DataFrame(data={'id_municipio': [5300108, 5300108, 5300108]})
df['id_municipio'] = df['id_municipio'].astype(str).str[:-1].astype(int)

If it's a str type column, the approach can be reduced to:

import pandas as pd
df = pd.DataFrame(data={'id_municipio': ['5300108', '5300108', '5300108']})
df['id_municipio'] = df['id_municipio'].str[:-1]

Upvotes: 1

Related Questions