Extract First Sub-Element from a List in R

Nizamuddin Siddiqui
Updated on 06-Mar-2021 14:15:37

13K+ Views

To extract only first element from a list, we can use sapply function and access the first element with double square brackets. For example, if we have a list called LIST that contains 5 elements each containing 20 elements then the first sub-element can be extracted by using the command sapply(LIST,"[[",1).Example1Consider the below data frame − Live DemoList1

Find Monthly Average from Different Date Data in R

Nizamuddin Siddiqui
Updated on 06-Mar-2021 14:15:10

2K+ Views

To find the monthly average from different date data, we first need to extract the months and year from date column and then find the average using aggregate function. For example, if we have a data frame called df which has three columns say x, month, and year then the monthly average can be found by using the command −aggregate(x~Month+Year,df,mean)Example1Consider the below data frame − Live DemoDate1

Change Code 'Yes' to 1 in R Data Frame Column

Nizamuddin Siddiqui
Updated on 06-Mar-2021 14:07:30

15K+ Views

To change the code “Yes” to 1, we can use ifelse function and set the Yes to 1 and others to 0. For example, if we have a data frame called df that contains a character column x which has Yes and No values then we can convert those values to 1 and 0 using the command ifelse(df$x=="Yes",1,0).Example1Consider the below data frame − Live DemoAgree

Count Particular Character in String Vector in R

Nizamuddin Siddiqui
Updated on 06-Mar-2021 14:02:00

173 Views

To find the count of a particular character in a string vector we can use nchar function along with gsub. For example, if we have a vector called x that contains string such India, Russia, Indonesia then we can find the number of times character i occurred then we can use the command nchar(gsub("[^i]","",x)) and the output will be 1 1 1 because first I’s in India and Indonesia will not be considered as they are in uppercase.Example1 Live Demox1

Find Mean Squared Error for Linear Model in R

Nizamuddin Siddiqui
Updated on 06-Mar-2021 14:01:33

4K+ Views

To find the mean squared error for linear model, we can use predicted values of the model and find the error from dependent variable then take its square and the mean of the whole output. For example, if we have a linear model called M for a data frame df then we can find the mean squared error using the command mean((df$y-predict(M))^2).Example1Consider the below data frame − Live Demox1

Create Data Frame Column with Letters of Both Size in R

Nizamuddin Siddiqui
Updated on 06-Mar-2021 13:57:25

404 Views

To create a data frame column with letters of both sizes, we can simply use the letters function and LETTERS function, the first one corresponds to lowercase letters and the latter corresponds to uppercase letters with single square brackets as shown in the below examples.Example1 Live Demodf1

Convert Matrix to Binary Matrix in R

Nizamuddin Siddiqui
Updated on 06-Mar-2021 13:55:48

2K+ Views

To convert a matrix to binary matrix, we can use as.matrix function and converting the matrix to logical matrix then adding 0 to the values greater than 0. For example, if we have a matrix called M then it can be converted to a binary matrix using the command −as.matrix((M

Find Index of Nearest Smallest Number in R Data Frame Column

Nizamuddin Siddiqui
Updated on 06-Mar-2021 13:55:20

203 Views

To find the index of the nearest smallest number in an R data frame column, we can use which function along with subsetting for the value for which we want to find the index of the nearest smallest number. To understand how it can be done check out the below examples.Example1Consider the below data frame − Live DemoID

Create a Date Vector with Randomization in R

Nizamuddin Siddiqui
Updated on 06-Mar-2021 13:49:43

241 Views

To create a date vector with randomization, we can use sample function but the dates need to be read with as.Date function. For example, if we have 2 dates say 2021-01-01 and 2021-02-02 then a vector with randomization of these three dates can be created by using the command −sample(c(as.Date("2021-01-01"),as.Date("2021-02-02")),100,replace=TRUE)Example1 Live DemoDate1

Multiply Large Numbers with All Digits in R

Nizamuddin Siddiqui
Updated on 06-Mar-2021 13:48:11

263 Views

To multiply large numbers with all digits in the output, we can use mul.bigz function of gmp package. For example, if we have two vectors say x and y each containing numbers of large size then the multiplication of these numbers that will return all the digits of the multiplication can be done by using the command mul.bigz(x,y).Example1Loading gmp package and multiplying vectors containing large number values −library(gmp) x1

Advertisements