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]
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
– Vinicius Soares
for its function the input would be a vector. Type
c(x1, x2, x3, x4)
– Jorge Mendes
Correct, but what if besides the x result, my code has 2inputs, how do I do the Paste in this case? for each input.
– Vinicius Soares