简体   繁体   中英

How do I convert integers to numeric and have 0s to the hundredths?

Given the following dataframe, I want to convert the first column, data_2018 to be numeric and have digits to the hundredths

structure(list(data_2018 = c(4, 4, 4, 4, 4, 4, NaN, NaN, NaN), 
    data_2017 = c(NA, "+0.33", "+0.58", "+0.24", "+0.67", "+0.83", 
    NA, NA, NA), a_mean = c("+0.59", "+0.26", "+0.5", "+0.4", 
    "+0.51", "+0.39", NA, NA, NA), b_mean = c("+0.54", "+0.27", 
    "+0.55", "+0.31", "+0.62", "+0.37", NA, NA, NA)), row.names = c(NA, 
-9L), class = "data.frame")

I've tried the following, but neither seem to work:

df[1] <- lapply(df[1], as.numeric)
df[1] <- round(df[1], digits = 2)

This is the block of code that I run to generate df . Not sure how to tweak this to round the first column, so if I can manage to do it in here, that would be nicer and cleaner:

round_df <- function(x, digits) {
  # round all numeric variables
  # x: data frame 
  # digits: number of digits to round
  numeric_columns <- sapply(x, mode) == 'numeric'
  x[numeric_columns] <-  round(x[numeric_columns], digits)
  x
}

df <- as.data.frame(cbind(data_2018 = round_df(old_df$data_2018, 2), 
                                     sapply(c("data_2017", "a_mean", "b_mean"), 
                                            function(c) (round_df(old_df$data_2018 - df[[c]], 2)))))

I've tried editing the old_df before this block as well, but keep getting returned the same integers

Old_df:

structure(list(data_name = structure(1:9, .Label = c("A", 
"B", "C", 
"D", "E", 
"F", "G", 
"H", "I"
), class = "factor"), data_2018 = c(4, 4, 4, 4, 4, 4, NaN, NaN, 
NaN), b_mean = c(3.4613889058431, 3.72575757720254, 3.45000000560985, 
3.68731343212412, 3.38352940888966, 3.6260294248076, 3.10442119411633, 
3.66819985569986, 3.46452822147108), a_mean = c(3.40740741623773, 
3.74117646497839, 3.49967318422654, 3.59940157217138, 3.48692812639124, 
3.60947714132421, 3.45057719920105, 3.55519480519481, 3.58463203390955
), data_2017 = c(NaN, 3.67499995231628, 3.41666662693024, 3.7619047164917, 
3.33333325386047, 3.16666662693024, 3.3589743742576, 3.55769230769231, 
3.32051282662612), tool = c("e", "e", "e", "e", "e", 
"e", "y", "y", "y"), status = c(NA, 6, 6, 6, 
6, 6, NA, NA, NA)), row.names = c(2L, 3L, 4L, 5L, 8L, 9L, 1L, 
6L, 7L), class = "data.frame")

Hope this helps. Just whatever I've mentioned in the comments.

old_df <- structure(list(data_name = structure(1:9, .Label = c("A", 
                                                               "B", "C", 
                                                               "D", "E", 
                                                               "F", "G", 
                                                               "H", "I"
), class = "factor"), data_2018 = c(4, 4, 4, 4, 4, 4, NaN, NaN, 
                                    NaN), b_mean = c(3.4613889058431, 3.72575757720254, 3.45000000560985, 
                                                     3.68731343212412, 3.38352940888966, 3.6260294248076, 3.10442119411633, 
                                                     3.66819985569986, 3.46452822147108), a_mean = c(3.40740741623773, 
                                                                                                     3.74117646497839, 3.49967318422654, 3.59940157217138, 3.48692812639124, 
                                                                                                     3.60947714132421, 3.45057719920105, 3.55519480519481, 3.58463203390955
                                                     ), data_2017 = c(NaN, 3.67499995231628, 3.41666662693024, 3.7619047164917, 
                                                                      3.33333325386047, 3.16666662693024, 3.3589743742576, 3.55769230769231, 
                                                                      3.32051282662612), tool = c("e", "e", "e", "e", "e", 
                                                                                                  "e", "y", "y", "y"), status = c(NA, 6, 6, 6, 
                                                                                                                                  6, 6, NA, NA, NA)), row.names = c(2L, 3L, 4L, 5L, 8L, 9L, 1L, 
                                                                                                                                                                    6L, 7L), class = "data.frame")



old_df[is.na(old_df)] <- NA

old_df$data_2018 <- sprintf("%.2f",old_df$data_2018)

old_df
#>   data_name data_2018   b_mean   a_mean data_2017 tool status
#> 2         A      4.00 3.461389 3.407407        NA    e     NA
#> 3         B      4.00 3.725758 3.741176  3.675000    e      6
#> 4         C      4.00 3.450000 3.499673  3.416667    e      6
#> 5         D      4.00 3.687313 3.599402  3.761905    e      6
#> 8         E      4.00 3.383529 3.486928  3.333333    e      6
#> 9         F      4.00 3.626029 3.609477  3.166667    e      6
#> 1         G        NA 3.104421 3.450577  3.358974    y     NA
#> 6         H        NA 3.668200 3.555195  3.557692    y     NA
#> 7         I        NA 3.464528 3.584632  3.320513    y     NA

Created on 2019-02-14 by the reprex package (v0.2.1)

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