Found 2038 Articles for R Programming

How to get the combinations for a range of values with repetition in R?

Nizamuddin Siddiqui
Updated on 16-Mar-2021 11:58:44

118 Views

The combination of values with repetition is the combination where the values can be repeated when creating the combination. For example, if we have three values say 1 and 2 then the combination of these values with repetition will be as follows −1 1 2 1 1 2 2 2For this purpose, we can use expand.grid function as shown in the below examples.Example 1 Live Demoexpand.grid(rep(list(1:2),2))Output  Var1 Var2 1  1   1 2  2   1 3  1   2 4  2   2Example2 Live Demoexpand.grid(rep(list(1:2),3))Output  Var1 Var2 Var3 1  1   1    1 2  2   1    1 3  1   2    1 4  2   2    1 5  1   1    2 6  2   1    2 7  1   2    2 8  2   2    2Example3 Live Demoexpand.grid(rep(list(1:2),4))Output  Var1 Var2 Var3 Var4 1  1   1    1    1 2  2   1    1    1 3  1   2    1    1 4  2   2    1    1 5  1   1    2    1 6  2   1    2    1 7  1   2    2    1 8  2   2    2    1 9  1   1    1    2 10 2   1    1    2 11 1   2    1    2 12 2   2    1    2 13 1   1    2    2 14 2   1    2    2 15 1   2    2    2 16 2   2    2    2Example4 Live Demoexpand.grid(rep(list(1:2),5))Output  Var1 Var2 Var3 Var4 Var5 1  1   1    1    1    1 2  2   1    1    1    1 3  1   2    1    1    1 4  2   2    1    1    1 5  1   1    2    1    1 6  2   1    2    1    1 7  1   2    2    1    1 8  2   2    2    1    1 9  1   1    1    2    1 10 2   1    1    2    1 11 1   2    1    2    1 12 2   2    1    2    1 13 1   1    2    2    1 14 2   1    2    2    1 15 1   2    2    2    1 16 2   2    2    2    1 17 1   1    1    1    2 18 2   1    1    1    2 19 1   2    1    1    2 20 2   2    1    1    2 21 1   1    2    1    2 22 2   1    2    1    2 23 1   2    2    1    2 24 2   2    2    1    2 25 1   1    1    2    2 26 2   1    1    2    2 27 1   2    1    2    2 28 2   2    1    2    2 29 1   1    2    2    2 30 2   1    2    2    2 31 1   2    2    2    2 32 2   2    2    2    2

How to find the sum of every n values in R data frame columns?

Nizamuddin Siddiqui
Updated on 16-Mar-2021 11:49:32

2K+ Views

To find the sum of every n values in R data frame columns, we can use rowsum function along with rep function that will repeat the sum for rows. For example, if we have a data frame called df that contains 4 columns each containing twenty values then we can find the column sums for every 5 rows by using the command rowsum(df,rep(1:5,each=4)).ExampleConsider the below data frame − Live Demox1

How to perform paired t test in R with a factor column in the data frame?

Nizamuddin Siddiqui
Updated on 16-Mar-2021 11:45:05

2K+ Views

When we have a factor column in an R data frame that has two levels and a numerical column then we can apply paired-test on this data frame but the data must be collected for same subjects, otherwise it will not be a paired data. The t.test application on the data discussed here can be done by using the command t.test(y1~x1,data=df), where y1 is the numerical column, x1 is the factor column, and both these columns are stored in data frame called df.ExampleConsider the below data frame − Live Demox1

How to extract the last element in an R matrix?

Nizamuddin Siddiqui
Updated on 16-Mar-2021 11:40:07

558 Views

To extract the last element in an R matrix, we can use the length function along with single square brackets that are used for subsetting. For example, if we have a matrix called M as shown below −M1 2 3 4 5 6 7 8 9then we can extract last value of M by using the command M[length(M)].ExampleConsider the below matrix − Live DemoM1

How to extract data frame columns stored in a list in R?

Nizamuddin Siddiqui
Updated on 16-Mar-2021 11:35:36

3K+ Views

Suppose we have two frames each having 5 columns that are stored in a list in R and the data that belongs to same columns has some kind of inherent relationship or we want to check whether there exists a relationship between them then we might want to extract those columns. Therefore, we can use lapply function for this extraction. For example, if we have a list called LIST that store two data frames then column 3 of each data frame can be extracted by using the command lapply(LIST, "[", 3).ExampleConsider the below data frames and list of these data ... Read More

How to remove starting and ending zeros in an R vector?

Nizamuddin Siddiqui
Updated on 16-Mar-2021 11:31:54

528 Views

To remove starting and ending zeros in an R vector, we can use min and max function to access values except 0 and then subsetting with single square brackets. For example, if we have a vector called x then we can remove starting and ending zeros by using the command −x[min(which(x!=0)):max(which(x!=0))]Example Live Demox1

How to remove rows that contain at least one 0 in R?

Nizamuddin Siddiqui
Updated on 16-Mar-2021 11:29:13

3K+ Views

To remove rows that contain at least one 0, we can use single square brackets for subsetting rows with apply that will select rows that do not contain even one zero. For example, if we have a data frame called df then we can remove rows that contain at least one 0 can be done by using the command df[apply(df,1, function(x) all(x!=0)),].ExampleConsider the below data frame − Live Demox1

How to split a data frame using row number in R?

Nizamuddin Siddiqui
Updated on 16-Mar-2021 11:25:37

1K+ Views

To split a data frame using row number, we can use split function and cumsum function. The split function will split the rows and cumsum function will select the rows. For example, if we have a data frame called df that contains twenty rows then we can split into two data frames at row 11 by using the below command −split(df,cumsum(1:nrow(df)%in%11)).ExampleConsider the below data frame − Live Demox1

How to check if a vector exists in a list in R?

Nizamuddin Siddiqui
Updated on 16-Mar-2021 11:17:43

4K+ Views

To check if a vector exists in a list, we can use %in%, and read the vector as list using list function. For example, if we have a list called LIST and a vector called V then we can check whether V exists in LIST using the command LIST %in% list(V).ExampleConsider the below list − Live DemoList

How to display mean in a boxplot with cross sign in base R?

Nizamuddin Siddiqui
Updated on 16-Mar-2021 11:14:20

287 Views

To display mean in a boxplot with cross sign in base R, we can use the points function and pass the mean with pch = 4 that represents a star, also we can change the color to highlight the mean using col argument and the size of the start can be changed using lwd argument as shown in the below examples.Example Live Demox

Advertisements