item in r

To access a specific item in R, you can use indexing with square brackets []. For example:

main.r
my_vector <- c(1, 2, 3, 4, 5)
third_element <- my_vector[3]
print(third_element) 
82 chars
4 lines

This code will create a vector my_vector with values 1 through 5, and then extract the third element (which has the value 3) using indexing. The code will print the value of third_element to the console.

You can also use indexing to change the value of a specific element in a vector:

main.r
my_vector[4] <- 10
print(my_vector)   
39 chars
3 lines

This code changes the value of the fourth element in my_vector from 4 to 10, and then prints the modified vector to the console.

gistlibby LogSnag