Skip to main content

Usefull R Commands

Most useful R commands You should know right Now. Start learning one by one.






  • help() #give help regarding a command, e.g. help(hist) 
  • c() #concatenate objects, e.g. x = c(3,5,8,9) or y = c(”Jack”,”Queen”,”King”) 1:19 #create a sequence of integers from 1 to 19 
  • (…) #give arguments to a function, e.g. sum(x), or help(hist) 
  • […] #select elements from a vector or list, e.g. x[2] gives 5, x[c(2,4)] gives 5 9 for x as above matrix() #fill in (by row) the values from y in a matrix of 4 rows and 3 columns by giving #m = matrix(y,4,3,byrow=T) 
  • dim() #gives the number of rows and the number of columns of a matrix, or a data frame head() #gives the first 6 rows of a large matrix, or data frame 
  • tail() #gives the last 6 rows of a large matrix, or data frame 
  • m[ ,3] #gives the 3rd column of the matrix m 
  • m[2, ] #gives the 2nd row of the matrix m 
  • = or <- #assign something to a variable, e.g. x = c(”a”,”b”,”b”,”e”) 
  • == #ask whether two things are equal, e.g. x = c(3,5,6,3) and then x == 3 gives T F F T #Then y[x == 3] gives those entries of y where x equals 3, i.e. the 1st and 4th entry of y < #ask whether x is smaller than y, e.g. x < 6 in the example above gives True True False True > #ask whether x is larger than y 
  • & #logical „and‟ 
  • | #logical „or‟ 
  • sum() #get the sum of the values in x by sum(x) 
  • mean() #get the mean of the values in x by mean(x) 
  • median() #get the median of the values in x by median(x) 
  • sd() #get the standard deviation of the values in x  
  • var() #get the variance of the values in x 
  • IQR() #get the IQR of the values in x 
  • summary() #get the summary statistics of a single variable, or of all variables in a data frame round() #round values in x to 3 decimal places by round(x,3) 
  • sort() #sort the values in x by giving sort(x) 
  • unique() #get the non-duplicate values from a list, e.g. x = c(3,5,7,2,3,5,9,3) and then unique(x) #gives 3 5 7 2 9 
  • length(x) #gives the length of the vector x, which is 8 
  • hist() #create a histogram of the values in x by hist(x) 
  • stem() #create a stem and leaf plot of the values in x by stem(x) 
  • boxplot() #create a boxplot of the values in x by boxplot(x) 
  • plot() #scatterplot of x vs. y by plot(x,y); for more parameters see help(plot.default) cor() #gives the linear correlation coefficient 
  • lm() #fit a least squares regression of y (response) on x (predictor) by fit = lm(y~x) names() #get or set the names of elements in a R object. E.g. names(fit) will give the names of the R  #object named “fit”, or 
  • #get or set the names of variables in a data frame.  
  • fit$coef #gives the least squares coefficients from the fit above, i.e. intercept and slope fit$fitted #gives the fitted values for the regression fitted above 
  • fit$residuals #gives the residuals for the regression fitted above 
  • lines() #add a (regression) line to a plot by lines(x,fit$fitted) 
  • abline() #add a straight line to a scatterplot 
  • points() #add additional points (different plotting character) to a plot by points(x,y2,pch=5) scan() #read data for one variable from a text file, e.g. y = scan(”ping.dat”) #Don‟t forget to change to the appropriate directory first  
  • read.table() #read spreadsheet data (i.e. more than one variable) from a text file table() #frequency counts of entries, ideally the entries are factors(although   #it works with integers or even reals) 
  • write() #write the values of a variable y in a file data.txt by write(y,file=”data.txt”) log() #natural logarithm (i.e. base e) 
  • log10() #logarithm to base 10 
  • seq() #create a sequence of integers from 2 to 11 by increment 3 with seq(2,11,by=3) rep() #repeat n times the value x, e.g. rep(2,5) gives 2 2 2 2 2 
  • getwd() #get the current working directory. 
  • setwd() #change the directory to. E.g. setwd("c:/RESEARCH/GENE.project/Chunks/") dir() #list files in the current working directory 
  • search() #searching through reachable datasets and packages 
  • library() #link to a downloaded R package to the current R session. E.g. library(Biostrings) link to the #R package #called “Biostrings” which you had downloaded earlier onto your laptop 
  • Input and Display  
  • load("c:/RData/pennstate1.RData") #load a R data frame  
  • read.csv(filename="c:/stat251/ui.csv",header=T) #read .csv file with labels in first row x=c(1,2,4,8,16) #create a data vector with specified elements y=c(1:10) #create a data vector with elements 1-10 vect=c(x,y) #combine them into one vector of length 2n mat=cbind(x,y) #combine them into a n x 2 matrix mat[4,2] #display the 4th row and the 2nd column mat[3,] #display the 3rd row 
  • mat[,2] #display the 2nd column
  • subset(dataset,logical) #those objects meeting a logical criterion subset(data.df,select=variables,logical) #get those objects from a data frame that meet a  #logical criterion  
  • data.df[data.df=logical] #yet another way to get a subset x[order(x$B),] #sort a dataframe by the order of the elements in B x[rev(order(x$B)),] #sort the dataframe in reverse order  
  • Moving Around 
  • ls() #list the R objects in the current workspace rm(x) #remove x from the workspace 
  • rm(list=ls()) #remove all the variables from the workspace attach(mat) #make the names of the variables in the matrix or data frame  #available in the workspace 
  • detach(mat) #releases the names 
  • new=old[,-n] #drop the nth column 
  • new=old[-n,] #drop the nth row 
  • new=subset(old,logical) #select those cases that meet the logical condition complete = subset(data.df,complete.cases(data.df)) #find those cases with no missing values new=old[n1:n2,n3:n4] #select the n1 through n2 rows of variables n3 through n4) 
  • Data Manipulation 
  • x.df=data.frame(x1,x2,x3 ...) #combine different kinds of data into a data frame scale() #converts a data frame to standardized scores round(x,n) #rounds the values of x to n decimal places ceiling(x) #vector x of smallest integers > x 
  • floor(x) #vector x of largest interger < x 
  • as.integer(x) #truncates real x to integers (compare to round(x,0) as.integer(x < cutpoint) #vector x of 0 if less than cutpoint, 1 if greater than cutpoint) factor(ifelse(a < cutpoint, "Neg", "Pos")) #is another way to dichotomize and to make a factor for analysis  transform(data.df,variable names = some operation) #can be part of a set up for a data set  
  • Statistical Tests 
  • binom.test() 
  • prop.test() #perform test with proportion(s) 
  • t.test() #perform t test 
  • chisq.test() #perform Chi-square test 
  • pairwise.t.test()  
  • power.anova.test() 
  • power.t.test() 
  • aov() 
  • anova() 
  • TukeyHSD() 
  • kruskal.test() 
  • Distributions  
  • sample(x, size, replace = FALSE, prob = NULL) # take a simple random sample of size n from the  # population x with or without replacement 
  • rbinom(n,size,p) 
  • pbinom() 
  • qbinom() 
  • dbinom() 
  • rnorm(n,mean,sd) #randomly generate n numbers from a Normal distribution with the specific mean and sd pnorm() #find probability (area under curve) of a Normal(10,3^2) distribution to the left  #of 8,i.e. P(X <= 8), by pnorm(8,mean=10,sd=3) 
  • qnorm() #find quantity or value x such that area under Normal(10,3^2) curve and to the left  #of x equals 0.25 by qnorm(0.25,mean=10,sd=3) 
  • rt() 
  • pt() 
  • qt() 
  • runif(n,lower,upper) 
  • punif() 
  • qunif()

Menu card just for you !

Select Your favourite topic to learn.

Comments

Trending⚡

Understanding link.click() in JavaScript

Hey! Today i am going to share with you how to use link.click() function in javascript As a JavaScript developer, you may come across the need to programmatically click a link on a web page. The link.click() method is a commonly used way to do this, and it is important to understand how it works and when to use it. What is link.click()? link.click() is a method that can be used to simulate a click on a link element in JavaScript. It is typically used when you want to trigger a link click event programmatically, rather than requiring the user to physically click the link. How to use link.click() Using link.click() is relatively straightforward. First, you need to select the link element you want to click using a DOM selector such as getElementById() or querySelector(). Then, you can call the click() method on the link element to simulate a click. Here is an example: // select the link element const myLink = document.getElementById('my-link'); // simulate a cl...

How to Create Studio Ghibli-Style AI Images on ChatGPT for Free

How to Create Studio Ghibli-Style AI Images on ChatGPT for Free AI-generated art is making waves across the internet, captivating audiences with stunning, ethereal visuals inspired by the iconic animation style of Studio Ghibli . These AI-crafted images, from dreamy landscapes to expressive characters, reflect the timeless magic of Hayao Miyazaki ’s beloved films such as Spirited Away , My Neighbor Totoro , and Howl’s Moving Castle . Thanks to recent advancements in AI technology, particularly OpenAI ’s latest ChatGPT update, users can now create their own Studio Ghibli-inspired illustrations effortlessly by entering simple text prompts. This exciting feature is transforming digital art creation and making it accessible to both professionals and beginners. In this article, we’ll guide you through creating Ghibli-style AI images using ChatGPT and explore free alternatives for users who don’t yet have access to this feature. Ghibli AI generator free Step-by-Step Guide: How to Crea...

Value Model vs Reference Model

Value Model vs Reference Model In programming languages, two different models are used for variables.  These are:  Value Model  A variable contains a value. The name of the variable gives its value.  Reference Model A variable contains (say y) refers to another variable (say x) with a value. The variable ‘y’ is used to access the value of ‘x’ indirectly.  The ‘C’ language is based on value model. However, by using pointers, we can implement the reference model. The pointer is used to access the value of a variable indirectly. Also Read Static and Dynamic Memory Allocation Memory Leak and Dangling Pointer Memory Allocation for 2D Array Dynamic Memory Allocation Pointer Constant and Constant Pointer Pointer Declarations and their Meanings Functions and Pointers Initializing Pointer to Pointer Pointer to Pointer Multiple Indirection Relationship between Array and Pointer Pointer to Array Pointer Arithmetic Types of Pointer Illustrat...

How to write programs in Bhai language

Bhai Language Bhai language is fun Programming language , with this language you can makes jokes in hindi. Bhai language written in typescript. It's very funny , easy and amazing language. Keywords of this language written in Hindi . Starting and ending of the program Start program with keyword " hi bhai " and end with " bye bhai ". It's compulsory to add this keyword before starting and end on the program. You write your programming logic inside this hi bhai and bye bhai . How to declare variables in Bhai language We use " bhai ye hai variable_name" keyword for declaring variables. In javascript we use var keyword for declaring variables but here you have to use " bhai ye hai " keyword. If you are declaring string then use " " double quotes. You can use Boolean variable like sahi and galat for true and false . How to print output in Bhai language You have to use " bol bhai " keyword for ...

ChatGPT Now Allows Free Users to Create Ghibli-Style AI Images – Here’s How

OpenAI has finally expanded its native image generation feature to free ChatGPT users, allowing them to transform images into stunning Studio Ghibli-style artwork . While the company has yet to make an official announcement, multiple tests using free ChatGPT accounts confirm that the feature is now accessible without requiring a paid subscription. Ghibli AI Images Now Available for Free Users Previously, OpenAI restricted its image generation capabilities to ChatGPT Plus, Pro, and Team users. This led free-tier users to seek alternatives like xAI’s Grok and Google’s Gemini . However, these tools often lacked the same level of detail and artistic refinement as OpenAI’s model. Now, with the rollout extending to free users, everyone can experience the magic of Ghibli-style AI transformations. How to Create Ghibli-Style AI Images with ChatGPT If you want to turn your photos into Ghibli-inspired masterpieces, follow these simple steps: Visit ChatGPT – Open the ChatGPT website or...

Quiz tells you what type of wife you want

What Type of Wife are You Looking For? Attend this quiz and know your wife expections Quiz reveals type of wife you expect, lets answer carefully... Personality Questions: 1. What type of personality are you looking for in a wife? Quiet and reserved Outgoing and social Intelligent and witty 2. What type of sense of humor are you looking for in a wife? Dry and sarcastic Witty and clever Playful and silly 3. What type of interests are you looking for in a wife? Intellectual and educational Creative and artistic Athletic and outdoorsy Values Questions: 4. What type of family values are you looking for in a wife? Traditional and conservative Open-minded and progressive Balanced and equal 5. What type of political views...