简体   繁体   中英

Changing single column with mixed character and numbers into just numerical in rstudio

I'm working on a project for school that needs to use Rstudio for data analysis. We have some columns with one character and numerics. We are trying to change this into just numerics. FYI We are not experienced users in any programming languages.

For example: We have a dataset with the following variables: Period - TPeriod - Brand etc Although Tperiod is all in numerics, both period and brand are not. They each include a single letter. Ex: Period b20160399992 (meaning 2016/march)

Brand b007 (meaning a brand name) (There are 56 brands, all following the same sequence -- b001 to b056)

We want to change the brand column or set a keyword for the brand column so that we can use b007 as just 7.

We think by changing this into just 7, we can compare them to their sale units/price etc.

If anyone could help, that would be greatly appreciated.

thank you

we have tried to use dplyr but could not follow through. again we are not experienced in any programming languages as we are just learning R for data analysis.

This is a two step procedure (although they can be combined for convenience).

First you need to take a substring of your variable that gets rid of characters you don't want, for the Brand variable this is the first character.

Then you want to convert this to numeric .

Assuming your dataframe is called df you can do this by taking a substring of the variable and converting it to numeric.

library(dplyr)

df <- df %>%
      mutate(brand_numeric = as.numeric(substr(Brand, 2)))

This is a commonly asked question though and you should get in the habit of searching the forums for solutions first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM