Extract Factor Levels from Factor Column in R Data Frame

Nizamuddin Siddiqui
Updated on 09-Feb-2021 12:21:43

13K+ Views

To extract the factor levels from factor column, we can simply use levels function. For example, if we have a data frame called df that contains a factor column defined with x then the levels of factor levels in x can be extracted by using the command levels(df$x). This extraction is helpful if we have a large number of levels.Example1 Live DemoConsider the below data frame −x1

Use Column Index Instead of Column Name in dplyr Group By

Nizamuddin Siddiqui
Updated on 09-Feb-2021 12:21:06

993 Views

When we use group_by function of dplyr package, we need to pass the column name(s) that are categorical in nature. If we want to use the index of the same column(s) then group_by_at function needs to be used, where we can pass the column index as the argument.Example1 Live DemoConsider the below data frame −x1 1 A 2 2 B 6 3 C 5 4 D 7Example2 Live Demoy1

Find Sum of Rows, Columns, and Total in a Matrix in R

Nizamuddin Siddiqui
Updated on 09-Feb-2021 12:20:45

10K+ Views

To find the sum of row, columns, and total in a matrix can be simply done by using the functions rowSums, colSums, and sum respectively. The row sums, column sums, and total are mostly used comparative analysis tools such as analysis of variance, chi−square testing etc.Example1 Live DemoM1

Find Number of Unique Values in a Vector Excluding Missing Values in R

Nizamuddin Siddiqui
Updated on 09-Feb-2021 12:13:10

1K+ Views

If there exist missing values in an R vector then it is counted as a unique value in the vector, therefore the extraction of unique values cannot be done directly. For this purpose, we need to use unique with na.omit function. For example. If we have a vector called x with missing values then the extraction of unique values can be done as length(unique(na.omit(x))).Example1 Live Demox1

Multiply All Values in a List by a Number in R

Nizamuddin Siddiqui
Updated on 09-Feb-2021 12:12:55

4K+ Views

To multiply all values in a list by a number, we can use lapply function. Inside the lapply function we would need to supply multiplication sign that is * with the list name and the number by which we want to multiple all the list values. For example, if we have a list called LIST and we want to multiply each value in LIST by 2 then it can be done by using the command lapply(LIST,"*",2).Example1 Live DemoList1

Create Column with Largest Size String Value in R Data Frame

Nizamuddin Siddiqui
Updated on 09-Feb-2021 12:10:44

184 Views

To create a column with largest size string value in rows, we can use apply function and define the size of the string for the largest value by creating a function as shown in the below examples. If the number of characters in all the columns are same or there exists some ties then the output will be the first one.Example1 Live DemoConsider the below data frame −x1

Create Frequency Table in R Including Zero Frequency

Nizamuddin Siddiqui
Updated on 09-Feb-2021 12:10:29

2K+ Views

When we use table function in R, the output shows the frequency of values that are available in the vector or in column of the data frame. If we want to create the table with the frequency zero for values that are not part of the vector or the column then first we need to convert them to factor first and then use the table function.Example1 Live Demox1

Find Correlation of One Variable with All Other Variables in R

Nizamuddin Siddiqui
Updated on 09-Feb-2021 12:09:00

3K+ Views

To find the correlation of each variable with remaining variables, we can create a correlation matrix but for the correlation of only one variable with all the other variables we need to define the columns inside the cor function. The output will represent the columns and rows as passed inside the function.Example1 Live DemoConsider the below data frame −x1

Find Sum of Squared Deviations for an R Data Frame Column

Nizamuddin Siddiqui
Updated on 09-Feb-2021 12:08:45

2K+ Views

The sum of squared deviations is the total of the square of difference between each value and the mean. To find this value, we need to create the formula in R platform. For example, if we have a data frame called df that contains a column x then the sum of squared deviations for x can be calculated by using sum((df$x−mean(df$x))^2).Example1 Live DemoConsider the below data frame −set.seed(1021) x1

Change Variable Names in a List in R

Nizamuddin Siddiqui
Updated on 09-Feb-2021 12:02:47

980 Views

The name of variables in a list are actually the list elements. These elements can be either named or unnamed. The naming can be done with the help of names function and renaming can be done in the same way as well. For example, if we have a list called LIST then the names of the element in LIST can be done by using the below command: names(LIST)

Advertisements