Beruflich Dokumente
Kultur Dokumente
A Data frame is a list of vectors of equal length. Data frame in R is used for storing data tables.
R code :
names(students_df)<-c("Course","Score")
students_df[1,2] # Access first row and second column of the data frame
#The structure of the data frame in R can be seen by using str() function
str(students_df)
Usage of Row bind rbind() function and column bind cbind() function to add the Row and Column to the
data frame.
Can join multiple vectors to create a data frame using the cbind() function.
#add an column
attempt <- c ("1st","1st","2nd","1st")
student_table<-cbind(student_df, attempt)
print(student_table)
The Apply family comprises: apply, lapply , sapply, vapply, mapply, rapply, and tapply. The Family of
Apply functions pertains to the R base package, and is populated with functions to manipulate slices of
data from matrices, arrays, lists and data frames in a repetitive way. Apply Function in R are designed to
avoid explicit use of loop constructs. They act on an input list, matrix or array, and apply a named
function with one or several optional argument.
a) an aggregating function, like for example the mean, or the sum (that return a number or scalar);
b) other transforming or sub-setting functions;
c) and other vectorized functions, which return more complex structures like list, vectors, matrices
and arrays.
1. Apply Function in R:
Returns a vector or array or list of values obtained by applying a function to margins of an array
or matrix.
example :
Age<-c(56,34,67,33,25,28)
Weight<-c(78,67,56,44,56,89)
Height<-c(165, 171,167,167,166,181)
BMI_df<-data.frame(Age,Weight,Height)
BMI_df
apply(BMI_df,1,sum)
apply(BMI_df,2,sum)
lapply function in R:
lapply function takes list, vector or Data frame as input and returns only list as output.
lapply(BMI_df, mean)
sapply function in R
sapply function takes list, vector or Data frame as input. It is similar to lapply function but
returns only vector as output.
sapply(BMI_df, mean)# applies mean function to the columns of the dataframe and the output
will be in the form of vector
mapply function in R:
mapply is a multivariate version of sapply. mapply applies FUN to the first elements of each (…)
argument, the second elements, the third elements, and so on.
i.e. For when you have several data structures (e.g. vectors, lists) and you want to apply a
function to the 1st elements of each, and then the 2nd elements of each, etc., coercing the
result to a vector/array as in sapply
mapply(sum, 1:4, 1:4, 1:4) #mapply sums up all the first elements(1+1+1) ,sums up all the second
elements(2+2+2) and so on so the result will be
rapply function in R:
rapply function in R is nothing but recursive apply, as the name suggests it is used to apply a
function to all elements of a list recursively.3
# rapply function in R
x=list(1,2,3,4)
rapply(x,function(x){x^2},class=c("numeric"))
vapply function in R:
vapply function in R is similar to sapply, but has a pre-specified type of return value, so it can be safer
(and sometimes faster) to use.
# vapply function in R
Arguments
one or more R objects, to be concatenated
…
together.
paste('one',2,'three',4,'five')
When multiple arguments are passed to paste, it will vectorize the operation, recycling shorter elements
when necessary. This makes it easy to generate variable names with a common prefix
The sep= argument controls what is placed between each set of values that are combined, and the
collapse= argument can be used to specify a value to use, when joining those individual values to create
a single string.
paste('X',1:5,sep='')
output: “X1” “X2” “X3” “X4” “X5”
output: "X_1 and Y_2 and X_3 and Y_4 and X_5"