Alexis Begni
Alexis Begni

Reputation: 106

Apply a function within another, using the same list in R

I need help, I'm tryin to run a function within another function with the same df but an error message always appears when I run the code

Here's below a sample of my df

X <- c("Bloc", "Mp", "xcible",  "ycible", "xO", "yO", "xN", "yN", "xE", "yE", "xS", "yS", "ID_ligne")
C <- c("LE-01", 22, 2.404024, -39.5192, -7.296761, -43.66444, -8.135254, -0.8619174, 6.710631, 0.20917, 7.339786, -41.44318, "LE-01-B-006-22")
D <- c("LE-01", 20, 1.362262, -40.59871, -7.296761, -43.66444, -8.135254, -0.8619174, 6.710631, 0.20917, 7.339786, -41.44318, "LE-01-B-006-20")
E <-c("LE-01", 22, 2.404024, -39.5192, -7.296761, -43.66444, -8.135254, -0.8619174, 6.710631, 0.20917, 7.339786, -41.44318, "LE-01-B-005-22")
G <- c("LE-01", 20, 1.362262, -40.59871, -7.296761, -43.66444, -8.135254, -0.8619174, 6.710631, 0.20917, 7.339786, -41.44318, "LE-01-B-005-20")
H <- c("LE-01", 24, 1.710175, -33.79383, -7.296761, -43.66444, -8.135254, -0.8619174, 6.710631, 0.20917, 7.339786, -41.44318, "LE-01-B-004-24") 
J <- c("LE-01", 22, 2.404024, -39.5192, -7.296761, -43.66444, -8.135254, -0.8619174, 6.710631, 0.20917, 7.339786, -41.44318, "LE-01-B-003-22")

Ex <- rbind(C,D,E,G,H,J)
colnames(Ex) <- X
Ex <- as.data.frame(Ex)
Ex$xO <- as.numeric(as.character(Ex$xO))
Ex$xS <- as.numeric(as.character(Ex$xS))
Ex$xN <- as.numeric(as.character(Ex$xN))
Ex$xE <- as.numeric(as.character(Ex$xE))
Ex$yO <- as.numeric(as.character(Ex$yO))
Ex$yS <- as.numeric(as.character(Ex$yS))
Ex$yN <- as.numeric(as.character(Ex$yN))
Ex$yE <- as.numeric(as.character(Ex$yE))
Ex.list <- split(Ex, f = Ex$ID_ligne)

Here's next the function which is in the bigger I try to run

eq_affine <- function(x1, y1, x2, y2){
  pente <- (y2 - y1)/(x2 - x1)
  int <- y2 - pente*x2
  return(c(pente, int))

The bigger function but reduced to the line when the error message occurs

 exp_lin_fun_rect <- function(xO, xS, xE, xN, 
                             yO, yS, yE, yN,
                             xcible, ycible,
                             xcompe, ycompe){

  eqSE <- eq_affin(xS, yS, xE, yE)
  eqEN <- eq_affine(xE, yE, xN, yN)
  eqNO <- eq_affine(xN, yN, xO, yO)
  eqOS <- eq_affine(xO, yO, xS, yS)
  return(data.frame(cbind(eqSE, eqEN, eqNO, eqOS)))
}

And the code I tried to run :

Ex <- unlist(lapply(Ex.list, exp_lin_fun_rect))

Error message is always Error in eq_affine(xS, yS, xE, yE) : argument "yE" is missing, with no default

No need to have the rest of the bigger function because the error occurs in the first row of the code. And I'm using a list because I have a tons of data and it's easier for me to work that way

Thanks for the help

Upvotes: 0

Views: 88

Answers (1)

hello_friend
hello_friend

Reputation: 5798

Slightly amended your code:

# Set up original dataframe like so: 

Ex <- setNames(data.frame(rbind(C,D,E,G,H,J), stringsAsFactors = FALSE), X)

# Clean data: 

Ex <- data.frame(lapply(Ex,
       function(x) {
         if(suppressWarnings(sum(is.na(as.numeric(x)))) == sum(is.na(x))){
           x <- as.numeric(x)
         } else{
           as.character(x)
         }
       }))

# Define function:
eq_affine <- function(x1, y1, x2, y2){
  pente <- (y2 - y1)/(x2 - x1)
  int <- y2 - pente*x2
  return(c(pente, int))
}


# Define function: 
exp_lin_fun_rect <- function(xO, xS, xE, xN, yO, yS, yE, yN, xcible, ycible,
                             xcompe, ycompe){
  eqSE <- eq_affine(xS, yS, xE, yE)
  eqEN <- eq_affine(xE, yE, xN, yN)
  eqNO <- eq_affine(xN, yN, xO, yO)
  eqOS <- eq_affine(xO, yO, xS, yS)

  return(list(eqSE = eqSE, eqEN = eqEN, eqNO = eqNO, eqOS = eqOS))

}

# Apply function: 
exp_lin_fun_rect(Ex$xO, Ex$xS, Ex$xE, Ex$xN, Ex$yO, Ex$yS, Ex$yE, Ex$yN, 
                 Ex$xcible, Ex$ycible, Ex$xcompe, Ex$ycompe)

Data:

X <- c("Bloc", "Mp", "xcible",  "ycible", "xO", "yO", "xN", "yN", "xE", "yE", "xS", "yS", "ID_ligne")
C <- c("LE-01", 22, 2.404024, -39.5192, -7.296761, -43.66444, -8.135254, -0.8619174, 6.710631, 0.20917, 7.339786, -41.44318, "LE-01-B-006-22")
D <- c("LE-01", 20, 1.362262, -40.59871, -7.296761, -43.66444, -8.135254, -0.8619174, 6.710631, 0.20917, 7.339786, -41.44318, "LE-01-B-006-20")
E <-c("LE-01", 22, 2.404024, -39.5192, -7.296761, -43.66444, -8.135254, -0.8619174, 6.710631, 0.20917, 7.339786, -41.44318, "LE-01-B-005-22")
G <- c("LE-01", 20, 1.362262, -40.59871, -7.296761, -43.66444, -8.135254, -0.8619174, 6.710631, 0.20917, 7.339786, -41.44318, "LE-01-B-005-20")
H <- c("LE-01", 24, 1.710175, -33.79383, -7.296761, -43.66444, -8.135254, -0.8619174, 6.710631, 0.20917, 7.339786, -41.44318, "LE-01-B-004-24") 
J <- c("LE-01", 22, 2.404024, -39.5192, -7.296761, -43.66444, -8.135254, -0.8619174, 6.710631, 0.20917, 7.339786, -41.44318, "LE-01-B-003-22")

Upvotes: 2

Related Questions