Found 2038 Articles for R Programming

How to find the sum based on a categorical variable in an R data frame?

Nizamuddin Siddiqui
Updated on 19-Nov-2020 07:41:20

2K+ Views

Finding group-wise mean is a common thing but if we go for step-by-step analysis then sum of values are also required when we have a categorical variable in our data set. This can be easily done with the help of group_by and summarise_each function of dplyr package.ExampleConsider the below data frame:Live Demo> Group Salary Emp EmpOutputGroup Salary 1 D 28256 2 B 31092 3 A 23147 4 C 28209 5 B 37676 6 C 33374 7 D 44864 8 B 40152 9 A 25843 10 A 40946 11 D 23321 12 A 42854 13 C 36960 14 A 35285 15 ... Read More

How to find the mean of list elements without unlisting them in R?

Nizamuddin Siddiqui
Updated on 19-Nov-2020 07:38:02

209 Views

Most of the times, unlisting is used to find the mean of list elements but we can also use double-square brackets for the same purpose. The double-square brackets are basically used to access the values in the elements of the list, thus mean function works with those values directly. Look at the below example to understand how it works.ExampleConsider the below list:Live Demo> x xOutput[1] 3 3 3 5 3 1 4 7 5 4 5 9 9 7 4 3 6 2 4 3 3 4 7 4 4 [26] 4 5 3 4 4 3 5 7 2 ... Read More

How to extract the names of vector values from a named vector in R?

Nizamuddin Siddiqui
Updated on 19-Nov-2020 07:32:32

4K+ Views

How to extract the names of vector values from a named vector in R?The names of vector values are created by using name function and the names can be extracted by using the same function. For example, if we have a vector called x that contains five values(1 to 5) and their names are defined as first, second, third, fourth and fifth then the names of values in x can be extracted by using names(x)[x==1].Example1Live Demo> x1 names(x1) x1Outputone two three four 1 2 3 4Example> names(x1)[x1==1]Output[1] "one"Example> names(x1)[x1==2]Output[1] "two" Example> names(x1)[x1==3]Output[1] "three"Example> names(x1)[x1==4]Output[1] "four" Example2Live Demo> x2 x2Output [1] ... Read More

How to perform cartesian join for two data.table objects in R?

Nizamuddin Siddiqui
Updated on 19-Nov-2020 07:27:49

1K+ Views

The cartesian join is the joining of two objects that creates the combination of each value in object with all the values in the other object. For example, if we have a vector x that contains 1, 2, 3 and the other object y contains a, b, c then the cartesian join will be 1a, 2a, 3a, 1b, 2b, 3b, 1c, 2c, and 3c. Check out the below examples to understand how it can be done.Example> library(data.table) > DT1 DT1Output x 1: 1 2: 2 3: 3 4: 4Example> DT2 DT2Output y 1: 25 2: ... Read More

How to find the column number of minimum values in each row for a data frame in R?

Nizamuddin Siddiqui
Updated on 19-Nov-2020 07:25:26

558 Views

To find the column number of minimum values in each row for a data frame, we can use apply function but if we want to return the output in tabular form then matrix function should be used. For example, if we have a data frame df then our problem can be solved by using the code: as.matrix(apply(df, 1, which.min)).ExampleConsider the below data frame:Live Demo> set.seed(37) > x1 x2 x3 x4 x5 df1 df1Outputx1 x2 x3 x4 x5 1 1 2 4 9 3 2 0 5 8 10 4 3 1 3 8 6 1 4 1 5 5 8 ... Read More

How to replace numbers with ordinal strings for a survey in an R vector?

Nizamuddin Siddiqui
Updated on 19-Nov-2020 07:22:16

1K+ Views

The easiest way to replace numbers with ordinal strings is using ifelse function. The ifelse function in R works as ifelse(test_expression, x, y). Here, test_expression must be a logical vector or an object that can be coerced to logical). The return value is a vector with the same length as test_expression.Example1Live Demo> x1 x1Output[1] 1 1 1 0 1 0 0 1 1 1 0 0 1 0 1 0 0 1 1 0 0 1 1 1 1 0 0 0 0 1 1 1 0 1 1 0 1 [38] 0 1 1 0 0 1 0 1 ... Read More

How to create a blank csv file in R?

Nizamuddin Siddiqui
Updated on 19-Nov-2020 07:19:09

451 Views

We can create a blank csv file using a single line code in R and the function that can do this is cat. If we want to have the file blank then NULL value will be passed inside the function and the file name must be used. For example, if we want to create a blank file named as BlankCSV then it can be created by using the below code:> cat(NULL, file="BlankCSV.csv")Output:This is the output from documents folder of the system where all the R files are stored by default(we can change that location if we want to):The output of ... Read More

How to check if values in a column of an R data frame are increasingly ordered or not?

Nizamuddin Siddiqui
Updated on 19-Nov-2020 07:17:00

196 Views

The values are increasingly ordered if the first value is less than the second, the second is less than the third, the third is less than the fourth, the fourth is less than the fifth, and so on. In base R, we have a function called is.unsorted that can help us to determine whether the values in a column of an R data frame are increasingly ordered or not. Check out the below examples to understand how it works.Example1Live Demo> set.seed(3257) > x df1 df1Output x 1 9 2 8 3 8 4 7 5 10 6 2 7 ... Read More

How to find the union of three vectors in R?

Nizamuddin Siddiqui
Updated on 19-Nov-2020 07:14:01

672 Views

The union function in base R helps us to find the union of two vectors but if we have three vectors then the union cannot be directly created. For this purpose, we need to use union function twice. For example, if we have three vectors defined as x, y, and z then the union of these vectors can be found by using the command union(x, union(y, z)).Example1Live Demo> x1 y1 z1 union(x1, union(y1, z1))Output[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Example2Live Demo> x2 x2Output[1] 13 6 16 11 9 11 3 15 ... Read More

How to display the curve on the histogram using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 19-Nov-2020 05:21:30

3K+ Views

Mostly, we use histogram to understand the distribution of a variable but if we have an overlay line on the histogram that will make the chart smoother, thus understanding the variation will become easy. To display the curve on the histogram using ggplot2, we can make use of geom_density function in which the counts will be multiplied with the binwidth of the histogram so that the density line will be appropriately created.ExampleConsider the below data frame:Live Demo> x df head(df, 20)Output x 1 4 2 5 3 6 4 4 5 9 6 ... Read More

Advertisements