Function Creation

I am very new to R and would like to know how a function is created.

Tell me if I have:

> colourName [1] "red" "green" "blue" "yellow" "white" "black" #Which the following colours equal something like this: #red = 1 #green = 2 #blue = 3 #yellow = 4 #white = 5 #black = 6 

How to create a function called myColour() , where the result is returned as a numeric vector?

So, if I introduce the following below, I should get:

 > myColour("yellow") [1] 4 

Please, help..

My code (but its wrong!)

 colourName<-c("red", "green", "blue", "yellow", "white", "black") data <- c(1,2,3,4,5,6) myFunction <- function(colour){ colourName = data return(colour) } myFunction("red") 

Is it possible to create it as a function?

+6
source share
4 answers

You can find your color in the named vector, which you can do so efficiently ...

 x <- setNames( seq_along( colourName ) , colourName ) #red green blue yellow white black # 1 2 3 4 5 6 x[ 'red' ] #red # 1 

Using the function here doesn't look like what needs to be done, but if you wanted to, you could have a simple search function like this that takes a color vector and search value (but you can also just use which !!) .. .

 myFunction <- function( colours , x){ y <- which( colours %in% x ) if( length(y) == 0L ) y <- "Colour not found" return( y ) } myFunction( colourName , "red") [1] 1 # Using R inbuilt colour names myFunction( colours() , "purple") [1] 547 
+6
source

This is why your code is wrong and what it does:

 colourName<-c("red", "green", "blue", "yellow", "white", "black") data <- c(1,2,3,4,5,6) 

which sets two variables as vectors with these values.

 myFunction <- function(colour){ 

which begins to define a new function with one parameter, colour .

 colourName = data 

which creates a new new variable called colourName , (the single = sign matches the <- sign) and gives the value data . This colourName variable is visible only inside the function. So now you have the variable colourName in the function whose value is c(1,2,3,4,5,6) , since its copy of data is outside the function.

 return(colour) } 

this returns the colour value as a result of the function. Since this is the same as the parameter in myFunction <- function(colour){ and you have not changed the colour variable, you will simply return what you put!

 myFunction("red") 

This calls the function by setting the colour argument to "red" . Now go through the function code and you will see that it will print [1] "red" - a vector identical to the input.

I know that this does not completely answer your question about how to get the number for the color you need, but you obviously do not understand a lot of the basics of programming, so I hope this helps.

+5
source

I assume that your difficulty is that you do not need a function, rather you need a named vector

You can create a vector for your example as follows. There may be faster ways to do this, but I hope this method is clear:

 colourName=c("red","green","blue","yellow","white","black") # Initialise a vector of numbers colours=1:length(colourName) # Name the vector elements according to the number-name relationship you're after names(colours)=colourName # See what the vector looks like colours # Try it out colours["yellow"] colours[["yellow"]] 

UPDATE: in short, to provide the same level of performance as the answers from @ SimonO101. Please note that this solution (and the second solution from @ SimonO101) does not require a function definition.

+1
source
 somevector <- c("red","blue","green") datavector <- c(1,2,3) mycolour <- function(whichcolour) { grep(x = somevector, pattern = whichcolour) } 

Output -

 > datavector[mycolour("green")] [1] 3 
0
source

Source: https://habr.com/ru/post/956699/


All Articles