Home > Mobile >  Split numeric variables by decimals in R
Split numeric variables by decimals in R

Time:09-13

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:

  1. If class(dat$Total) is numeric, 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.

  2. If class(dat$Total) is character, then

    dat <- 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   10
    

    The two new columns are also character. For this, you may want one of two more steps:

    1. Removing leading 0s, and keep them character:

      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   10
      
    2. Convert 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.integer if 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
  • Related