Skip to content

Instantly share code, notes, and snippets.

@furqon
Created June 18, 2021 03:44
Show Gist options
  • Save furqon/e2ff3deca354d270be54732433bb00f7 to your computer and use it in GitHub Desktop.
Save furqon/e2ff3deca354d270be54732433bb00f7 to your computer and use it in GitHub Desktop.
Inbuilt Function in R

Inbuilt Function in R

Basic

var = dataframe str(var) get data structure of dataframe head(var, n=x) get the first x data (x default 6) tail(var, n=x) get the last x data (x default 6) table(tblx$colName) get the freq of dataframe from categorical column min(tblx$colName) get the min value of the column max(tblx$colName) get the max value of the column mean(tblx$colName) get the mean value of the column range(tblx$colName) get the min-max value of the column

Condition

if

if(tblx$ColumnName[1]>4) {
	print("do something")
}

if-else

if(tblx$ColumnName[1] > someVal) {
   	print("do something")
} else {
  print("do something else")
}

Looping

for loop vec <- c(1:5) do something to each in vec

for (i in vec) {
	print(i + 5)
}

while loop

i=1 #define i
while(i<10) { # break when i < 10
	print(i+5)
	i=i+1
}

User defined function

basically user can have function to do certain task, using function()

adding5 <- function(x) {
	return(x + 5)
}
something <- adding5(30)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment