Found 2038 Articles for R Programming

Write a C program to work on statements using functions and loops

Bhanu Priya
Updated on 10-Mar-2021 09:36:53

78 Views

ProblemHow to print the long lines into two or more short lines based on specified length in the program using C Language?SolutionLet’s write a code to read a long line and print into two or more short lines according to the mentioned size in the program.The built in functions that we take in this program readline() function, is used to store text in array and returns the size of line.The logic we use to read a short sentence is explained below −while((charcter=readtext())>0){    if(charcter>length){       count=0;       a=0;       while(alength){          count=0;          a=0;          while(a

How to convert a vector to data frame in R by defining number of columns?

Nizamuddin Siddiqui
Updated on 05-Mar-2021 05:55:19

173 Views

If we have a vector where alternate values may create a tabular form then we might want to convert the vector into a data frame. For this purpose, we first need to convert the vector into a matrix with appropriate number of columns/rows and then read it as a data frame using as.data.frame function. Check out the below examples to understand how it works.Example1Live Demo> x1 x1Output[1] "1" "male" "1" "male" "1" "male" "1" "male" [9] "1" "male" "1" "male" "1" "male" "1" "male" [17] "1" "male" "1" "male" "2" "female" "2" "female" [25] "2" "female" "2" "female" "2" "female" ... Read More

How to display upper and lower quartiles through different line in a boxplot in R?

Nizamuddin Siddiqui
Updated on 04-Mar-2021 07:52:21

123 Views

To display the upper and lower quartiles through different line in base R boxplot, we can use abline function but we need to find the quartiles inside abline using quantile for the respective quartiles. The lines created by using abline and quantiles and the boxplot function may not coincide because of the differences in calculation. The calculation method for boxplot is explained below −The two ‘hinges’ are versions of the first and third quartile. The hinges equal the quartiles for odd n (where n x boxplot(x)OutputExample> abline(h=quantile(x,c(0.25,0.75)),col="blue")Output

How to find the frequency table for factor columns in an R data frame?

Nizamuddin Siddiqui
Updated on 05-Mar-2021 05:57:39

613 Views

If we have factor columns in an R data frame then we want to find the frequency of each factor level for all the factor columns. This can be done with the help of sapply function with table function. For example, if we have a data frame called df that contains some factor columns then the frequency table for factor columns can be created by using the command sapply(df, table).Example1Consider the below data frame −Live Demo> x1 x2 df1 df1Output   x1 x2 1  D  a 2  D  b 3  D  c 4  D  b 5  D  c 6  C  a ... Read More

How to find the row-wise mode of a matrix in R?

Nizamuddin Siddiqui
Updated on 05-Mar-2021 06:11:00

461 Views

There is no in-built function to find the mode in R, hence we need to create one and then apply it to the rows of the matrix. The function for mode is created as follows −mode M1 M1Output     [,1] [,2] [,3] [,4] [,5] [1,]  2    2    1    2   2 [2,]  2    2    2    2   1 [3,]  2    2    1    1   1 [4,]  2    1    1    1   1 [5,]  2    1    1    2   2> apply(M1,1,mode)Output[1] 2 2 1 1 2Example2Live Demo> M2 M2Output     [,1] [,2] [,3] [,4] [,5] [1,]  1    1    2    2    1 [2,]  2    1    1    2    1 [3,]  2    2    1    1    1 [4,]  2    1    1    2    2 [5,]  2    1    1    2    2 [6,]  1    2    1    1    2 [7,]  1    1    2    1    2 [8,]  2    2    1    2    1 [9,]  2    1    1    2    2 [10,] 1    1    2    2    2 [11,] 1    1    2    1    2 [12,] 1    2    2    2    1 [13,] 2    2    2    2    1 [14,] 2    1    2    2    1 [15,] 1    2    1    1    2 [16,] 2    2    1    2    1 [17,] 2    2    1    1    1 [18,] 2    1    1    2    1 [19,] 1    1    1    2    1 [20,] 2    1    1    2    2> apply(M2,1,mode)Output[1] 1 1 1 2 2 1 1 2 2 2 1 2 2 2 1 2 1 1 1 2Example3Live Demo> M3 M3Output     [,1] [,2] [,3] [,4] [,5] [1,]  1    3    3    2    1 [2,]  2    3    1    2    2 [3,]  2    2    3    3    1 [4,]  1    3    1    3    2 [5,]  3    1    2    1    2 [6,]  2    3    1    1    1 [7,]  2    2    2    3    1 [8,]  1    2    2    2    2 [9,]  2    1    2    1    2 [10,] 1    3    1    2    1 [11,] 2    1    3    1    1 [12,] 1    1    3    2    2 [13,] 2    1    1    1    2 [14,] 2    1    3    3    2 [15,] 1    2    3    1    2 [16,] 1    2    1    2    1 [17,] 3    1    1    3    2 [18,] 3    3    3    3    1 [19,] 3    2    3    1    1 [20,] 3    3    2    2    1> apply(M3,1,mode)Output[1] 1 2 2 1 1 1 2 2 2 1 1 1 1 2 1 1 1 3 1 2Example4Live Demo> M4 M4Output      [,1] [,2] [,3] [,4] [,5] [1,]  10    10   9    10   9 [2,]  9     9    10   9    9 [3,]  9     9    9    10   10 [4,]  10    9    9    10   10 [5,]  10    10   9    10   9 [6,]  10    10   9    10   10 [7,]  9     9    9    10   9 [8,]  9     10   9    10   9 [9,]  9     9    9    9    9 [10,] 9     10   9    10   9 [11,] 10    10   9    9    9 [12,] 9     9    9    9    9 [13,] 10    10   10   9 10 [14,] 10    9    10   10 10 [15,] 9     10   9    10 9 [16,] 9     10   9    10 9 [17,] 9     10   10   9 10 [18,] 9     9    9    9 10 [19,] 10    9    9    10 9 [20,] 10    9    9    10 9> apply(M4,1,mode)Output[1] 10 9 9 10 10 10 9 9 9 9 9 9 10 10 9 9 10 9 9 9

How to create group names for consecutively duplicate values in an R data frame column?

Nizamuddin Siddiqui
Updated on 05-Mar-2021 06:12:19

345 Views

The grouping of values can be done in many ways and one such way is if we have duplicate values or unique values then the group can be set based on that. If all the values are unique then there is no sense for grouping but if we have varying values then the grouping can be done. For this purpose, we can use rleid function as shown in the below examples.Example1Consider the below data frame −Live Demo> x df1 df1Output x 1 2 2 1 3 2 4 2 5 1 6 ... Read More

How to convert a data frame into table for two factor columns and one numeric column in R?

Nizamuddin Siddiqui
Updated on 05-Mar-2021 06:15:52

382 Views

When we have two factor columns and one numeric column then we can create a contingency table for the total count of numeric values based on the factor columns. This can be done with the help of xtabs function in base R. For example, if we have a data frame called df that contains two factor columns say f1 and f2, and one numeric column say Y then the contingency table for df can be created by using the command xtabs(Y~f1+f1, df).Example1Consider the below data frame −Live Demo> x1 x2 y1 df1 df1Output   x1 x2 y1 1  B  a  5 ... Read More

How to change the legend shape using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 11-Feb-2021 12:17:30

1K+ Views

By default, the shape of legend is circular but we can change it by using the guides function of ggplot2 package. For example, if we have a data frame with two numerical columns say x and y, and one categorical column Group then the scatterplot between x and y for different color values of categories in categorical column Group having different shape of legends can be created by using the below command −ggplot(df, aes(x, y, color=Group))+geom_point()+guides(colour=guide_legend(override.aes=list(shape=0)))Here, we can change the shape argument value to any value between starting from 0 to 25.Consider the below data frame −Example Live DemoxRead More

How to deal with error “undefined columns selected” while subsetting data in R?

Nizamuddin Siddiqui
Updated on 11-Feb-2021 12:14:37

2K+ Views

When we do subsetting with the help of single square brackets we need to be careful about putting the commas at appropriate places. If we want to subset rows using the columns then comma needs to be placed before the condition. The “undefined columns selected” error occurs when we do not specify any comma. Check out the examples to understand how it works.Consider the below data frame −Example Live Demox15),]Output   x1 x2 1  7  0 2  6  4 4  6  1 7  6  1 9  7  3 11 6  3 12 9  2 15 7  4 16 7  3 17 6  2 18 6  3Example Live Demoy1

How to collapse data frame rows in R by summing using dplyr?

Nizamuddin Siddiqui
Updated on 11-Feb-2021 12:05:26

3K+ Views

To collapse data frame rows by summing using dplyr package, we can use summarise_all function of dplyr package. For example, if we have a data frame called df that has a categorical column say Group and one numerical column then collapsing of rows by summing can be done by using the command −df%>%group_by(Group)%>%summarise_all(funs(sum))Consider the below data frame −Example Live DemoGroup

Advertisements