Found 2038 Articles for R Programming

How to create a column with largest size string value in rows in an R data frame?

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

94 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

How to create a frequency table in R that includes zero frequency for value that are not available?

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

1K+ 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

How to find the correlation of one variable with all the 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

How to find the sum of squared deviations for an R data frame column?

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

1K+ 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

How to convert a data frame with categorical columns to numeric in R?

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

891 Views

We might want to convert categorical columns to numeric for reasons such as parametric results of the ordinal or nominal data. If we have categorical columns and the values are represented by using letters/words then the conversion will be based on the first character of the category. To understand the conversion, check out the below examples.Example1 Live DemoConsider the below data frame −set.seed(100) x1

How to change the name of variables in a list in R?

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

673 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)

How to remove rows from data frame in R that contains NaN?

Nizamuddin Siddiqui
Updated on 09-Feb-2021 11:57:56

12K+ Views

The NaN values are referred to as the Not A Number in R. It is also called undefined or unrepresentable but it belongs to numeric data type for the values that are not numeric, especially in case of floating-point arithmetic. To remove rows from data frame in R that contains NaN, we can use the function na.omit.Example1 Live DemoConsider the below data frame −x1

How to check whether a column exists in an R data frame?

Nizamuddin Siddiqui
Updated on 09-Feb-2021 11:57:42

784 Views

If we have very large data set then it is highly that we forget the column names, therefore, we might want to check whether a particular column exists in the data frame or not if we know the column name. For this purpose, we can use grep function that will result the column name if exists in the data frame otherwise 0. To understand how it works check out the below examples.Example1 Live DemoConsider the below data frame −Gender

How to find the frequency of values greater than or equal to a certain value in R?

Nizamuddin Siddiqui
Updated on 09-Feb-2021 11:54:46

852 Views

In Data Analysis, we often need to look for less than, less than equal to, greater than, or greater than equal to values to compare them with some threshold. Sometimes we also require the frequency of these values. Therefore, we can use sum function for this purpose. For example, if a vector x has 10 integer values then to check how many of them are greater than or equal to 10, we can use the command sum(x>=10).Example1 Live Demox1=5)Output[1] 83Example2 Live Demox2=5)Output[1] 8Example3 Live Demox3=0.25)Output[1] 38Example4 Live Demox4=10)Output[1] 49Example5 Live Demox5=4)Output[1] 21

How to find the counts of categories in categorical columns in an R data frame?

Nizamuddin Siddiqui
Updated on 09-Feb-2021 11:48:37

5K+ Views

If we have two categorical columns in an R data frame then we can find the frequency/count of each category with respect to each category in the other column. This will help us to compare the frequencies for all categories. To find the counts of categories, we can use table function as shown in the below examples.Example1 Live DemoConsider the below data frame −x1

Advertisements