a function created does not work

#1
Hello, I had created a function and I aimed to verify if it complies with certain data and if it was, then it is stored in another variable.

Code:
data = c(12, 20, 11, 16, 18, 32, 22 , 8)
class = function(N){}
  tabla1= c(1:8)
  for (i in length(N)){
    if (data[i]<21.78){
      table1[i]= data[i]
    }
  }
  return(table1)
}
dats <- class(10) # data no return
 

obh

Active Member
#2
Hi Royer,

Did you try running the function in R ...

You closed the function before you doing anything ...
Code:
function(N){}
"For" loop should be in an array ... not a length of one number ...
Code:
> length(10)
[1] 1
I'm not sure what did you try to do, but you can write something like

Code:
> data = c(12, 20, 11, 16, 18, 32, 22 , 8)
> class = function(N){
+     table1 =c(1:8)
+     for (i in table1 ){
+         if (data[i]<21.78){
+             table1[i]= data[i]
+         }
+     }
+     return(table1)
+ }
>
>
> dats <- class(10)
> dats
[1] 12 20 11 16 18  6  7  8
 
#3
but what a silly mistake I made, thank you very much for your response
Hi Royer,

Did you try running the function in R ...

You closed the function before you doing anything ...
Code:
function(N){}
"For" loop should be in an array ... not a length of one number ...
Code:
> length(10)
[1] 1
I'm not sure what did you try to do, but you can write something like

Code:
> data = c(12, 20, 11, 16, 18, 32, 22 , 8)
> class = function(N){
+     table1 =c(1:8)
+     for (i in table1 ){
+         if (data[i]<21.78){
+             table1[i]= data[i]
+         }
+     }
+     return(table1)
+ }
>
>
> dats <- class(10)
> dats
[1] 12 20 11 16 18  6  7  8


but what a silly mistake I made, thank you very much for your response
 

obh

Active Member
#4
No worry, so I learned how to write a function in R :) similar to other program languages.
When running in R you should look on the message: "Error in length(N) : object 'N' not found"

You can also click the open bracket and the close bracket will be highlighted.