## Vectors
## -------
## Create a vector of numbers
x <- c(1,3,5,7)
y <- 1:4
z <- seq(2,8,by=2)
## Print x, y and z
class(x) # numeric
## Create a vector of named numbers
x <- c(a=1,b=3,c=5,d=7)
print(x)
names(x)
## Obtain vector length
length(x)
## Extract the 3rd element
x[3]
## Extract the last 2 elements
x[2:3]
## Or
x[2:length(x)]
## Extract the first and the last
x[c(1,3)]
## Or
x[c(1,length(x))]
## Extract the 3rd element without its name
x[[3]]
## Create a vector of strings
c("Bob", "Matilde", "Mr. Bean")
## Long vectors
x <- 1:1000
head(x)
tail(x)
## Matrices
## --------
## Create a matrix from a single vector
x <- 1:12
matrix(x)
matrix(x, ncol=3)
matrix(x, nrow=3)
matrix(x, ncol=3, byrow=TRUE) # Spread the x by rows instead of columns
m <- matrix(x, ncol=3)
class(m)
## Get the number of columns and rows
ncol(m)
nrow(m)
## Create a matrix from two or more vectors
x=1:10
y=11:20
z=101:110
# Bind vectors by rows
m <- rbind(x,y,z)
print(m)
# Bind vectors by columns
m <- cbind(x,y,z)
print(m)
## Remove column names
colnames(m) <- NULL
print(m)
## Use a vector of strings as column names
colnames(m) <- c("A", "B", "C")
print(m)
## Create a new matrix from an existing one
## New matrix using first and second row
m[1:2,]
## New matrix using first and third row
m[c(1,3),]
## New matrix using last two columns
m[ , c(2,ncol(m)) ]
## New matrix using first and third row and last two columns
m[c(1,3), c(2,ncol(m))]
## New matrix using column names
m[ , c("A","B") ]
## Create a new matrix from two existing ones
x <- 1:12
m <- matrix(x, ncol=3)
x <- 101:112
n <- matrix(x, ncol=3)
## Bind matrices by columns
cbind(m, n)
## Bind matrices by rows
rbind(m, n)
## DANGEROUS BEND!
## Dimension dropping
x <- 1:12
m <- matrix(x, ncol=3)
class(m)
n <- m[,1:2]
print(n)
class(n)
n <- m[,1] # subsetting with one row or colummn gives a vector
print(n)
class(n) # not a matrix any more
n <- m[,1, drop=FALSE] # if you don't want to drop a dimension
print(n)
class(n) # still a matrix
## Matrix cannot have both number and strings at the same time!
## In case of conflict, strings win
x=1:3
y=c("A", "B", "C")
cbind(x,y) # x is transformed in a string
## Data frames
## -----------
## Create a data frame from two or more vectors
df <- data.frame(x=1:10, y=11:20, z=101:110)
print(df)
class(df)
## Get the number of columns and rows
ncol(df)
nrow(df)
## Use a vector of strings as column names
colnames(df) <- c("A", "B", "C")
print(df)
## Create a new DF from an existing one
## New DF using first and second row
df[1:2,]
## New DF using first and third row
df[c(1,3),]
## New DF using last two columns
df[ , c(2,ncol(df)) ]
## You can omit leading comma to select columns (don't do this for matrices!)
df[ c(2,ncol(df)) ]
## New DF using first and third row and last two columns
df[c(1,3), c(2,ncol(df))]
## New data frame using column names
df[ , c("A","B") ]
## Create a new data frame from two existing ones
## Bind by column
cbind(df, df)
## Bind by rows
rbind(df, df)
## Data frames support both numbers and strings together
data.frame(x=1:3, y=c("A", "B", "C"))
## Convert a data frame to a matrix
df=cbind(df, df)
as.matrix(df)
## DANGEROUS BEND!
## Dimension dropping
df <- data.frame(x=1:3, y=c("A", "B", "C"))
class(df)
df2 <- df[1:2]
print(df2)
class(df2)
df2 <- df[,1] # subsetting with one row or colummn in matrix styles gives a vector
print(df2)
class(df2)
df2 <- df[1] # subsetting with one colummn without comma keeps the data frame class
class(df2)
df2 <- df[[1]] # subsetting with one colummn with double=brackets gives a vector
class(df2)
About Online Rscript Compiler
Try our Online Rscript Compiler (Version R v3.4.1) to Edit, Run, and Share your Rscript Code directly from your browser. This online development environment provides you the latest version R v3.4.1.
How to use Online Rscript Compiler?
Write and Execute Code
- Write your program (or, paste it) directly under the "Source Code" tab.
- If you want to save your program, go to the "Project" menu and save it.
- You can directly execute your program without saving it by clicking on on "Execute" button.
User Input
The latest version of Coding Ground allows to provide program input at run time from the termnial window exactly the same way as you run your program at your own computer. So simply run a program and provide your program input (if any) from the terminal window available in the right side.
Online Rscript Compiler: Keyboard Shortcuts
The following are the keyword shortcut of this Online Rscript Compiler:
Shortcut | Description |
⌘ + Enter | Run the program |
⌘ + S | Save Project (Login Required) |
⇧ + ⌘ + S | Save As Project |
⌘ + P | New Project |
⌘ + G | Share Project |
⌘ + Z | Undo Editing |
⌘ + Y | Redo Editing |
⌘ + A | Select All Text |
⌘ + X | Cut Selected Text |
⌘ + C | Copy Selected Text |
⌘ + V | Paste Copied Text |
⌘ + F | Search Text |
⌘ + ⌥ + F | Replace Text |
Shortcut | Description |
Ctrl + Enter | Run the program |
Ctrl + S | Save Project |
Shift + Ctrl + S | Save As Project |
Ctrl + G | Share Project |
Ctrl + Z | Undo Editing |
Ctrl + Y | Redo Editing |
Ctrl + A | Select All Text |
Ctrl + X | Cut Selected Text |
Ctrl + C | Copy Selected Text |
Ctrl + V | Paste Copied Text |
Ctrl + F | Search Text |
Ctrl + H | Replace Text |
Online Rscript Compiler: Save and Share Rscript Code (Project)
Save Rscript Project Online
You can save your Rscript Project with us so that you can access this project later on. To save a project you will need to create a login Id with us. So before you save a project, please create a login Id using a link given at the top right corner of this page.
Share Rscript Project Online
You can use this feature to share your Rscript Code with your teachers, classmates and colleagues. Just click Share Button and it will create a short link, which can be shared through Email, WhatsApp or even through Social Media. A shared link will be deleted if it has been passive for almost 3 months.
More Features of Online Rscript Compiler
- Theme – You can change the current editor's theme from the "Editor Theme" option under "Settings" menu.
- Font Size – You can change the font size of the editor /compiler from from the "Font Size" option under "Settings" menu.
- Tab Size – You can change the tab size from the "Tab Size" option under "Settings" Menu.
- Show/Hide Line Numbers – You can show/hide the line number with the code from the "Show Line Numbers" or "Hide Line Numbers" option under "Settings" Menu.
- And, many more.
Benefits of Using Online Rscript Compiler
There are several benefits of using the Online Rscript Compiler to run your Rscript code:
- Platform independence: You can run your code from any device without taking care of operating systems.
- Convenience: You don't need to install anything for using this.
- No setup required: There is no need for additional setup to run your code.
- Updated version: Our online compiler/editors/terminals are the latest up-to-date.