如何将函数应用于data.frame的每个元素/单元格?

时间:2016-06-04 21:04:14

标签: r dataframe lapply sapply

我想舍入到两个十进制数字值,这些值很小,而且整数值很大。我想从desired.df获得df

df <- data.frame(a = c(0.123, 123.456), b = c(321.321, 0.321))
desired.df <- data.frame(a = c(0.12, 123), b = c(321, 0.32))

df
#         a       b
# 1   0.123 321.321
# 2 123.456   0.321

desired.df
#        a      b
# 1   0.12    321
# 2    123   0.32

怎么做?我已经尝试了apply及其一些变体,显然不是正确的方式,以及嵌套*应用函数(如*apply(df, function(x) *apply(y, function(y) ...))),但没有运气......

例如,以下内容不起作用

round.my.way <- function(cell) {
if(cell < 2) return(round(cell, 2)) 
else return(round(cell, 0))
}

apply(df, 1:2, round.my.way)

有什么想法吗?

修改 我真的很抱歉,我没有强调发布的desired.df可能有更多关于在数据框中独立格式化每个“单元格”的事情,但我没有意识到这一点。

my.data.frame <- structure(list(mean = c(1.09586889552429, -0.488970742717109,                        
717.369670482251, 1430.22132153487), sd = c(14.6805055234266,                        
0.847924955832248, 3148.43113658712, 6224.1150344482), skew = c(15.4288196192469,    
-0.848099854154573, 15.6753519251057, 13.8872189273213), kurt = c(760.003399845995,  
3.42534233994167, 403.114382084221, 296.372235189872), min = c(0.0211657925274003, 
-6.63384131834086, 1.14376333333333, 1.51974013933333), X5.. = c(0.163422411626505, 
-1.94866860909688, 5.55001008333333, 8.48210945666667), X25.. = c(0.419624647887877, 
-0.940344070376478, 24.2286483333333, 40.2822858746667), median = c(0.712376790798036, 
-0.397878278400871, 79.5970091666667, 147.535862094), X75.. = c(1.11275078144806, 
0.0525922423468574, 300.39765875, 594.258755956833), X95.. = c(2.12897531610479, 
0.694437497721256, 2918.320405, 5614.78688014776), max = c(932.099715965617, 
3.36571976522158, 77776.31031, 151194.931773039), n = c(4046.23166666667, 
4046.23166666667, 4046.23166666667, 4046.23166666667)), .Names = c("mean", 
"sd", "skew", "kurt", "min", "X5..", "X25..", "median", "X75..", 
"X95..", "max", "n"), row.names = c(NA, 4L), class = "data.frame")

通过我的一个实验,我几乎得到了我想要的东西:

apply(my.data.frame, c(1, 2), function(x) if(x < 2) round(x, 2) else round(x, 0))

产生

     mean      sd  skew kurt   min  X5.. X25.. median  X75..   X95..    max
1    1.10   15.00 15.00  760  0.02  0.16  0.42   0.71   1.11    2.00    932
2   -0.49    0.85 -0.85    3 -6.63 -1.95 -0.94  -0.40   0.05    0.69      3
3  717.00 3148.00 16.00  403  1.14  6.00 24.00  80.00 300.00 2918.00  77776
4 1430.00 6224.00 14.00  296  1.52  8.00 40.00 148.00 594.00 5615.00 151195
     n
1 4046
2 4046
3 4046

确实将最后一个n列显示为不带小数的整数,但不显示其余列!

但我想改变这种细胞变化的格式:

     mean      sd  skew kurt   min  X5.. X25.. median  X75..   X95..    max
1    1.10      15    15  760  0.02  0.16  0.42   0.71   1.11    2.00    932
2   -0.49    0.85 -0.85    3 -6.63 -1.95 -0.94  -0.40   0.05    0.69      3
3     717    3148    16  403  1.14     6    24     80    300    2918  77776
4    1430    6224    14  296  1.52     8    40    148    594    5615 151195
     n
1 4046
2 4046
3 4046
4 4046

2 个答案:

答案 0 :(得分:1)

您的功能UIButton适用于round.my.way。对apply

行和列apply

使用c(1,2)的中间参数
apply(df,c(1,2),round.my.way)
      a      b
[1,]   0.12 321.00
[2,] 123.00   0.32

其他可能也有效,

假设1:如果您cell估价less than 2,那么round to 2 decimal places else 0 decimal places。这是基于您上面写的功能。

df <- data.frame(a = c(0.123, 123.456), b = c(321.321, 0.321))
df <- as.matrix(df)
df1 <- ifelse(df < 2,round(df,2),round(df))
df1
#         a      b
# [1,]   0.12 321.00
# [2,] 123.00   0.32
df1 <- as.data.frame(df1)

假设2:根据您的预期输出,我假设您所有diagonal elements (row number = column number) are rounded up to 2 decimal places并休息到0 decimal places。如果假设是正确的,那么下面将是解决方案之一:

  df <- data.frame(a = c(0.123, 123.456), b = c(321.321, 0.321))
  df <- as.matrix(df)
  df1 <- ifelse(row(df)==col(df),round(df,2),round(df))
  df1
  #      [,1]   [,2]
  # [1,]   0.12 321.00
  # [2,] 123.00   0.32
  df1 <- as.data.frame(df1)

您还可以使用floor在小数点前打印,但输出会将小数点后的位数打印为数据帧中最高的2或3,在这种情况下,我们有0和{ {1}} {}}} 2,如果prints 2 decimal place for all elementsround小数位,3prints 321.000df[1,2]为{ {1}}

123.000

小数点后3位,

df[2,1]

答案 1 :(得分:1)

使用data.table方法的快速解决方案

library(data.table)

setDT(df)[, lapply(.SD, function(x) ifelse(x < 2, round(x, 2), round(x) ))]

#>        a      b
#> 1:   0.12 321.00
#> 2: 123.00   0.32