Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
R (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Basic syntax === The following examples illustrate the basic [[programming language syntax|syntax of the language]] and use of the command-line interface.{{efn|An expanded list of standard language features can be found in the R manual, "An Introduction to R" {{URL|cran.r-project.org/doc/manuals/R-intro.pdf}}}} In R, the generally preferred [[Assignment (computer science)|assignment operator]] is an arrow made from two characters <code><-</code>, although <code>=</code> can be used in some cases.<ref>{{cite web|author=R Development Core Team|title=Assignments with the = Operator|url=https://developer.r-project.org/equalAssign.html|access-date=2018-09-11}}</ref> <syntaxhighlight lang="rout"> > x <- 1:6 # Create a numeric vector in the current environment > y <- x^2 # Create vector based on the values in x. > print(y) # Print the vector’s contents. [1] 1 4 9 16 25 36 > z <- x + y # Create a new vector that is the sum of x and y > z # Return the contents of z to the current environment. [1] 2 6 12 20 30 42 > z_matrix <- matrix(z, nrow = 3) # Create a new matrix that turns the vector z into a 3x2 matrix object > z_matrix [,1] [,2] [1,] 2 20 [2,] 6 30 [3,] 12 42 > 2 * t(z_matrix) - 2 # Transpose the matrix, multiply every element by 2, subtract 2 from each element in the matrix, and return the results to the terminal. [,1] [,2] [,3] [1,] 2 10 22 [2,] 38 58 82 > new_df <- data.frame(t(z_matrix), row.names = c("A", "B")) # Create a new data.frame object that contains the data from a transposed z_matrix, with row names 'A' and 'B' > names(new_df) <- c("X", "Y", "Z") # Set the column names of new_df as X, Y, and Z. > print(new_df) # Print the current results. X Y Z A 2 6 12 B 20 30 42 > new_df$Z # Output the Z column [1] 12 42 > new_df$Z == new_df['Z'] && new_df[3] == new_df$Z # The data.frame column Z can be accessed using $Z, ['Z'], or [3] syntax and the values are the same. [1] TRUE > attributes(new_df) # Print attributes information about the new_df object $names [1] "X" "Y" "Z" $row.names [1] "A" "B" $class [1] "data.frame" > attributes(new_df)$row.names <- c("one", "two") # Access and then change the row.names attribute; can also be done using rownames() > new_df X Y Z one 2 6 12 two 20 30 42 </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)