I have a data frame with a column that contains numeric values, which represent the price.
| ID | Total |
|---|---|
| 1124 | 12.34 |
| 1232 | 12.01 |
| 1235 | 13.10 |
I want to split the column Total by "." and create 2 new columns with the euro and cent amount. Like this:
| ID | Total | Euro | Cent |
|---|---|---|---|
| 1124 | 12.34 | 12 | 34 |
| 1232 | 12.01 | 12 | 01 |
| 1235 | 13.10 | 13 | 10 |
| 1225 | 13.00 | 13 | 00 |
The euro and cent column should also be numeric.
I tried:
df[c('Euro', 'Cent')] <- str_split_fixed(df$Total, "(\\.)", 2)
But I get 2 new columns of type character that looks like this:
| ID | Total | Euro | Cent |
|---|---|---|---|
| 1124 | 12.34 | 12 | 34 |
| 1232 | 12.01 | 12 | 01 |
| 1235 | 13.10 | 13 | 1 |
| 1225 | 13.00 | 13 |
If I convert the character columns (euro and cent) to numeric like this: as.numeric(df$Euro) the 00 cent value turns into NULL and the 10 cent turn into 1 cent.
Any help is welcome.
CodePudding user response:
Two methods:
If
class(dat$Total)isnumeric, you can do this:dat <- transform(dat, Euro = Total %/% 1, Cent = 100 * (Total %% 1)) dat # ID Total Euro Cent # 1 1124 12.34 12 34 # 2 1232 12.01 12 1 # 3 1235 13.10 13 10%/%is the integer-division operator,%%the modulus operator.If
class(dat$Total)ischaracter, thendat <- transform(dat, Euro = sub("\\..*", "", Total), Cent = sub(".*\\.", "", Total)) dat # ID Total Euro Cent # 1 1124 12.34 12 34 # 2 1232 12.01 12 01 # 3 1235 13.10 13 10The two new columns are also
character. For this, you may want one of two more steps:Removing leading
0s, and keep themcharacter:dat[,c("Euro", "Cent")] <- lapply(dat[,c("Euro", "Cent")], sub, pattern = "^0 ", replacement = "") dat # ID Total Euro Cent # 1 1124 12.34 12 34 # 2 1232 12.01 12 1 # 3 1235 13.10 13 10Convert to numbers:
dat[,c("Euro", "Cent")] <- lapply(dat[,c("Euro", "Cent")], as.numeric) dat # ID Total Euro Cent # 1 1124 12.34 12 34 # 2 1232 12.01 12 1 # 3 1235 13.10 13 10
(You can also use
as.integerif you know both columns will always be such.)
CodePudding user response:
Just use standard numeric functions:
df$Euro <- floor(df$Total)
df$Cent <- df$Total %% 1 * 100
