How to replicate a line with a difference of names on it? R

Asked

Viewed 57 times

1

I have a function and I didn’t want to have to create a new line in it whenever I add a new object to it:

a<- function(x1,x2,x3,x4){
res_x1= x1*4.95+x1
print("resultado x1")
print(res_x1)
res_x2= x2*4.95+x2
print("resultado x2")
print(res_x2)
res_x3= x3*4.95+x3
print("resultado x2")
print(res_x3)
res_x4= x4*4.95+x4
print("resultado x4")
print(res_x4)}
a(x1=10,x2=10,x3=10,x4=10)

My idea is at the time I have to add another variable X5 for example I don’t have to write the whole code for the X5.

res_x5= x5*4.95+x5
print("resultado x5")
print(res_x5)}

Is there any way to make r, copy a line

res_x1= x1*4.95+x1
print("resultado x1")
print(res_x1)"

and repeat it inside Function by changing the X1 names to the number of existing variables? ex, if you have 19 variables replicate this line 19 times but one with res_x5,res_x6,res_x7,rex_8,rex_9,rex_10,rex_11,rex_12,rex_13,rex_14,rex_15,rex_16,rex_17,rex_18,rex_19?

I was wondering if it is possible to create an input "quantity" and from this input the r replicates these lines with the amount of this input...

Thanks for the answers but I came up with another question if I have more than one input, if I use

for(i in 1:length(input1),i in 1:length(input2)
for(i in 1:length(input1,input2))

All these attempts make mistakes, can you loop more than one input? for example:

for(i in 1:length(input1,input2,input3))
  a <- (input1[i] * input2[i]) +z + g + h+i+ input3[i]

2 answers

1

You can use a loop for() with the function paste():

a <- function(input)
{
  for(i in 1:length(input))
  {
    res <- input[i] * 4.95 + input[i]
    print(paste0('resultado x', i))
    print(res)
  }  
}

a(input = c(10, 10, 10, 10))
[1] "resultado x1"
[1] 59.5
[1] "resultado x2"
[1] 59.5
[1] "resultado x3"
[1] 59.5
[1] "resultado x4"
[1] 59.5
  • And when I have more than 1 input to pass inside Function?? tried on a Function this and gave error Error in length() 4 Arguments passed to 'length' which requires 1

  • 1

    for its function the input would be a vector. Type c(x1, x2, x3, x4)

  • Correct, but what if besides the x result, my code has 2inputs, how do I do the Paste in this case? for each input.

0

You can use Ellipsis ... in its function. Within the argument of a function, ... allows the function to accept n arguments -with names or not. See how the function works in action:

foo <- function(...){
  dots <- list(...)

  if(length(dots) == 0) return(cat("Nenhum argumento fornecido."))

  for(i in seq_along(dots)){
    res <- dots[[i]] * 4.95
    msg <- paste0("Resultado ", names(dots[i]), ": ", res, ".\n")
    cat(msg)
  }
}

Whirling foo(x1=10, baz=1000, "boom"=19, 39) on the console, we have:

> foo(x1=10, baz=1000, "boom"=19, 39)
Resultado x1: 49.5.
Resultado baz: 4950.
Resultado boom: 94.05.
Resultado : 193.05.

To use ... within the function, just turn ... on a list using list(...). Above, you can see that I transformed ... on a list: dots <- list(...). In this case, dots is a class object list where each element is a given argument.

Notice:

inDots <- function(...){dots <- list(...);return(dots);}

On the console:

> inDots(x1=10, baz=1000, "boom"=19, 39)
$`x1`
[1] 10

$baz
[1] 1000

$boom
[1] 19

[[4]]
[1] 39

See how the function returns each argument in a list. From there, you only need to manipulate this list as we see in foo. It is important to note that depending on the type of result you want to get with function, you need to do an analysis of how you want to handle certain objects that may be provided in foo and that can "break" their function. Some examples are the use of NULL, NA and NaN.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.