如何将整数转换为数字,并且将0转换为百分之一?

时间:2019-02-14 15:16:11

标签: r

鉴于以下数据框,我想将第一列data_2018转换为数字并将位数转换为百分之一

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")

我尝试了以下方法,但似乎都没有用:

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

这是我运行以生成df的代码块。不知道如何调整它以使第一列变圆,因此,如果我可以在这里完成它,那将更好,更干净:

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)))))

我也尝试在此代码段之前编辑old_df,但始终返回相同的整数

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")

1 个答案:

答案 0 :(得分:0)

希望这会有所帮助。我在评论中提到的任何内容。

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

reprex package(v0.2.1)于2019-02-14创建