如何在R中应用函数后保留数据框结构?


当我们使用apply家族函数时,默认情况下输出不是数据框的形式。如果我们想保留原始数据框结构,我们需要通过设置apply家族函数的应用到原始数据框,使用单括号且不带参数,如下例所示。

示例1

考虑下面的数据框:

在线演示

> df1<-data.frame(x1=rpois(20,5),x2=rpois(20,2))
> df1

输出

   x1 x2
1  4  2
2  6  2
3  5  2
4  2  1
5  8  4
6  7  2
7  5  3
8  5  1
9  4  2
10 6  1
11 5  2
12 6  1
13 3  2
14 2  4
15 7  2
16 6  1
17 8  2
18 3  5
19 6  3
20 5  2

使用lapply将paste函数应用于df1:

> lapply(df1, function(x) paste(x,"%",sep=""))

输出

$x1
[1] "4%" "6%" "5%" "2%" "8%" "7%" "5%" "5%" "4%" "6%" "5%" "6%" "3%" "2%" "7%"
[16] "6%" "8%" "3%" "6%" "5%"

$x2
[1] "2%" "2%" "2%" "1%" "4%" "2%" "3%" "1%" "2%" "1%" "2%" "1%" "2%" "4%" "2%"
[16] "1%" "2%" "5%" "3%" "2%"

使用lapply将paste函数应用于df1并保留数据框结构:

> df1[]<-lapply(df1, function(x) paste(x,"%", sep=""))
> df1

输出

   x1 x2
1  4% 2%
2  6% 2%
3  5% 2%
4  2% 1%
5  8% 4%
6  7% 2%
7  5% 3%
8  5% 1%
9  4% 2%
10 6% 1%
11 5% 2%
12 6% 1%
13 3% 2%
14 2% 4%
15 7% 2%
16 6% 1%
17 8% 2%
18 3% 5%
19 6% 3%
20 5% 2%

示例2

在线演示

> df2<-data.frame(y1=rnorm(20),y2=rnorm(20))
> df2

输出

        y1         y2
1  -0.752047670  1.13712713
2  -0.128263190 -0.32853561
3  -0.302105388  0.13927555
4  -0.962792837 -0.11345683
5  -0.792733177 -0.44710830
6  -0.374832959 -0.11577915
7   0.006232538  1.69377281
8  -0.248837346  1.53856740
9  -1.329142492 -0.93981148
10 -0.800394342  0.23268426
11 -0.024673008 -1.45167348
12 -0.798652443  0.56592880
13  0.752464819  0.26117631
14  0.356222254  1.09788129
15 -0.189963724  0.04015303
16  1.237747888  0.30426806
17 -0.079703224 -1.19632730
18  0.610445704 -0.09226399
19  1.312108743  0.59445517
20  0.117974479  0.31822741

使用lapply将paste函数应用于df2:

> lapply(df2, function(x) paste(x,"*100",sep=""))

输出

$y1
[1] "-0.75204767044446*100" "-0.128263189709441*100"
[3] "-0.302105388264774*100" "-0.962792837263563*100"
[5] "-0.792733176975126*100" "-0.37483295904029*100"
[7] "0.0062325380738995*100" "-0.24883734622218*100"
[9] "-1.32914249184804*100" "-0.800394341521099*100"
[11] "-0.0246730078659625*100" "-0.798652442767393*100"
[13] "0.752464818654371*100" "0.356222253519168*100"
[15] "-0.189963724365201*100" "1.23774788828591*100"
[17] "-0.0797032238997077*100" "0.61044570354826*100"
[19] "1.31210874294046*100" "0.117974478781838*100"

$y2
[1] "1.13712713179116*100" "-0.328535609444004*100"
[3] "0.139275554888166*100" "-0.113456833535264*100"
[5] "-0.447108300367073*100" "-0.115779145719786*100"
[7] "1.69377280937111*100" "1.53856740099674*100"
[9] "-0.939811483903564*100" "0.232684264342039*100"
[11] "-1.45167347644544*100" "0.565928797260725*100"
[13] "0.261176305488481*100" "1.09788129483035*100"
[15] "0.0401530283786003*100" "0.304268061193212*100"
[17] "-1.1963272953655*100" "-0.0922639906523411*100"
[19] "0.594455172315153*100" "0.318227406098396*100"

使用lapply将paste函数应用于df2并保留数据框结构:

> df2[]<-lapply(df2, function(x) paste(x,"*100",sep=""))
> df2

输出

           y1                   y2
1  -0.75204767044446*100     1.13712713179116*100
2  -0.128263189709441*100   -0.328535609444004*100
3  -0.302105388264774*100    0.139275554888166*100
4  -0.962792837263563*100   -0.113456833535264*100
5  -0.792733176975126*100   -0.447108300367073*100
6  -0.37483295904029*100    -0.115779145719786*100
7   0.0062325380738995*100   1.69377280937111*100
8  -0.24883734622218*100     1.53856740099674*100
9  -1.32914249184804*100    -0.939811483903564*100
10 -0.800394341521099*100    0.232684264342039*100
11 -0.0246730078659625*100  -1.45167347644544*100
12 -0.798652442767393*100    0.565928797260725*100
13  0.752464818654371*100    0.261176305488481*100
14  0.356222253519168*100    1.09788129483035*100
15 -0.189963724365201*100    0.0401530283786003*100
16  1.23774788828591*100     0.304268061193212*100
17 -0.0797032238997077*100  -1.1963272953655*100
18  0.61044570354826*100    -0.0922639906523411*100
19  1.31210874294046*100     0.594455172315153*100
20  0.117974478781838*100    0.318227406098396*100

更新于:2021年3月5日

296 次浏览

启动你的职业生涯

完成课程获得认证

开始
广告