Apply user defined function across columns in R

Viewed 154

I have two functions in R that convert a radian and an angle into Cartesian coordinates as follows:

x_cart<-function(theta,r){
  return(r * cos (theta))
}
y_cart<-function(theta,r){
  return(r * sin (theta))
}

Then I want to apply this function to create two new columns in my data frame as x and y from the columns angle and radius. When I use lapply, I get an error of argument r is missing with no default.

df$x<-apply(df[,c("angle_adj","hit_distance")],1, x_cart())

Test data

angle<-c(10,15,20)
radius<-c(15,35,10)
df<-data.frame(angle,radius)
5 Answers

A tidyverse option.

library(dplyr)

df %>%
  mutate(X = x_cart(angle, radius),
         Y = y_cart(angle, radius))

#   angle radius          X         Y
# 1    10     15 -12.586073 -8.160317
# 2    15     35 -26.589077 22.760074
# 3    20     10   4.080821  9.129453

You don't need apply for this, use something like this:

x_cart<-function(theta,r){
  return(r * cos (theta))
}
y_cart<-function(theta,r){
  return(r * sin (theta))
}

angle<-c(10,15,20)
radius<-c(15,35,10)
df<-data.frame(angle,radius)

df$x <- x_cart(df$angle, df$radius)

# or using with()
df$x <- with(df, x_cart(angle, radius))

or even more compact:

cart <- function(theta,r){
  r * c(cos(theta), sin(theta))
}

df <- data.frame(
  angle = c(10, 15, 20),
  radius = c(15, 35, 10)
)

df[c("x", "y")] <- cart(df$angle, df$radius)

Updated

Thanks for the input provided by dear @ThomasIsCoding we can spare one extra line and also make use of base R's new native pipe:

sapply(c(x_cart,y_cart),function(f) mapply(f,df1$angle, df1$radius)) |>
  data.frame() |>
  cbind(df1) -> df2

colnames(df2) <- c("x", "y", "angle", "radius")
df2

           x         y angle radius
1 -12.586073 -8.160317    10     15
2 -26.589077 22.760074    15     35
3   4.080821  9.129453    20     10

You can also use this with tidyverse as a way of more automating the process:

library(dplyr)
library(purrr)

# First wrap our arguments in a list
lst <- list(df1$angle, df1$radius)

# Then we create a character vector of our function names
c("x_cart", "y_cart") %>%
  map(~ exec(.x, !!!lst)) %>%
  set_names(nm = c("x", "y")) %>%
  bind_cols() %>%
  bind_cols(df)

# A tibble: 3 x 4
       x     y angle radius
   <dbl> <dbl> <dbl>  <dbl>
1 -12.6  -8.16    10     15
2 -26.6  22.8     15     35
3   4.08  9.13    20     10

A base-R solution for the usage of apply. In this case it's better to redefine your function(s):

x_cart_2 <- function(input) { return(input[2] * cos(input[1])) }

df$x <- apply(df[,c("angle","radius")],1, x_cart_2)

returns

  angle radius          x
1    10     15 -12.586073
2    15     35 -26.589077
3    20     10   4.080821

Try sapply like below

cbind(
  df,
  sapply(
    setNames(c(x_cart, y_cart), c("x", "y")),
    function(f) with(df, f(angle, radius))
  )
)

which gives

  angle radius          x         y
1    10     15 -12.586073 -8.160317
2    15     35 -26.589077 22.760074
3    20     10   4.080821  9.129453
Related