Apply function to every value in R dataframe
Last Updated :
05 Apr, 2021
In R Programming Language to apply a function to every integer type value in a data frame, we can use lapply function from dplyr package. And if the datatype of values is string then we can use paste() with lapply. Let’s understand the problem with the help of an example.
Dataset in use:
|
A |
B |
C |
D |
1. |
1 |
8 |
21 |
4 |
2. |
9 |
2 |
0 |
6 |
3. |
6 |
3 |
14 |
3 |
4. |
5 |
6 |
5 |
7 |
5. |
9 |
4 |
3 |
1 |
6. |
6 |
3 |
2 |
3 |
after applying value*7+1 to each value of the dataframe
Expected result:
|
A |
B |
C |
D |
1. |
8 |
57 |
148 |
29 |
2. |
64 |
15 |
1 |
43 |
3. |
43 |
22 |
99 |
22 |
4. |
36 |
43 |
36 |
50 |
5. |
64 |
29 |
22 |
8 |
6. |
43 |
22 |
15 |
22 |
Method 1 : Using lapply function:
lapply is a function from apply family. By using lapply, we can avoid for loop as for loop is slower than lapply. lapply works faster than a normal loop because it doesn’t mess with the environment you work in. It returns output as a list. ‘l’ in lapply indicates list.
Syntax:
lapply(X, FUN, …)
Here, X can be a vector list or data frame. And FUN takes a function that you wish to apply to the data frame as an argument.
Approach:
- Create a dummy dataset.
- Create a custom function that you want to apply to every value in the data frame.
- Apply this custom function to every value in the data frame with the help of lapply.
- Display result
Example
R
m <- c (1,9,6,5,9,6)
n <- c (8,2,3,6,4,3)
o <- c (21,0,14,5,3,2)
p <- c (4,6,3,7,1,3)
df <- data.frame (A=m,B=n,C=o,D=p)
magic_fun <- function (x){
return (x*7+1)}
data.frame ( lapply (df,magic_fun))
|
Output :
Using lapply
Method 2 : Using paste and apply function:
paste() takes an R object as an argument and converts it to characters then paste it back with another string, i.e.it converts the argument to the character string and concatenates them.
Syntax:
paste (…, sep = ” “)
Our R object which is to be converted to string goes in place of “…”, sep=” “ represents a character string to separate the terms.
Approach:
- Create a dummy dataset.
- Apply the custom function which will print “Hello,” then value in the data frame value
- Display result
Example:
R
m <- c ( "Vikas" , "Varun" , "Deepak" )
n <- c ( "Komal" , "Suneha" , "Priya" )
df <- data.frame (A=m,B=n)
df[]<- data.frame ( lapply (df, function (x) paste ( "Hello," ,x,sep= "" )))
df
|
Output:
Using paste and apply
Method 3: Using purrr
purrr is a functional programming toolkit. Which comes with many useful functions such as a map. The map() function iterates across all entries of the vector and returns the output as a list. It allows us to replace for loop with in the code and makes it easier to read.
Syntax :
map(.x, .f) returns a list
map_df(.x, .f) returns a data frame
map_dbl(.x, .f) returns a numeric (double) vector
map_chr(.x, .f) returns a character vector
map_lgl(.x, .f) returns a logical vector
Here, .x is input and .f is a function that you want to be applied. Input to map function can be a list, a vector, or a data frame.
Note: You need to install purrr package explicitly using the following command.
install.packages(“purrr”)
Approach :
- Create a vector, a list and a data frame
- Create a custom function which you want to be applied.
- Use map() to apply custom function on vector, list and data frame.
- Display result
Working with integer datatype:
Program:
R
install.packages ( "purrr" )
library ( "purrr" )
x <- c (1,2,3,4,5,7,8,9)
y <- list (2,4,9,6,3,7,1,5,4)
df <- data.frame (A= c (1,2,3,4,5),B= c (6,7,8,9,10))
custom_f <- function (x){
return (x*2)
}
map_dbl (x,.f=custom_f)
map (y,.f=custom_f)
map_df (df,.f=custom_f)
|
Output:
For a vector :
Vector Output
For a list :
List output
For a data frame:
dataframe output
Working with character datatype:
Program:
R
library ( "purrr" )
x <- c ( "Red" , "Blue" , "Green" , "Yellow" , "Orange" )
y <- list ( "spring" , "summer" , "fall" , "winter" )
df <- data.frame (Working_days= c ( "Mon" , "Tues" , "Wednes" ),
off_days= c ( "Sun" , "Satur" , "Thurs" ) )
map_chr (x,paste0, " color" )
map (y,paste0, " season" )
map_df (df,paste0, "day" )
|
Output:
For a vector:
Vector as Output
For a list:
List as Output
For a data frame :
Dataframe as Output
Share your thoughts in the comments
Please Login to comment...