How to preserve data frame structure after applying a function in R?



When we apply a function using apply family, by default the output is not in the form of a data frame. If we want to preserve the original data frame structure then we need to set the application of the apply family by setting it to the original data frame with single brackets and no arguments as shown in the below examples.

Example1

Consider the below data frame −

Live Demo

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

Output

   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

Applying a function paste with lapply to df1 −

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

Output

$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%"

Applying a function paste with lapply to df1 and preserving the data frame structure −

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

Output

 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%

Example2

Live Demo

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

Output

 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

Applying a function paste with lapply to df2 −

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

Output

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

Applying a function paste with lapply to df2 and preserving the data frame structure −

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

Output

 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
Updated on: 2021-03-05T05:09:17+05:30

378 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements