In R, you can create variable names with patterns using the paste()
function along with string manipulation functions like gsub()
and regular expressions. Here's an example of creating a variable name based on a pattern:
main.r267 chars8 lines
In this example, we first create a vector fruits
containing the names of different fruits. We then use gsub()
to replace any spaces in the fruit names with underscores. Next, we use paste()
to concatenate the variable name pattern "fruit_name_"
with the modified fruit names. The sep
argument specifies that we want to separate the pattern and fruit names with an underscore.
The resulting variable name is a character vector that can be used to create a variable with that name:
main.r229 chars9 lines
This outputs three variable names starting with "fruit_name_" that have been created and assigned a value of 0. The ls()
function with the pattern
argument can be used to list all variables with a certain pattern, in this case fruit_name_
.
gistlibby LogSnag