R Programming Articles

Page 3 of 174

How to replicate whole data frame and add it in the original one in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 1K+ Views

The replicates of a data frame in R can be created with the help of sapply function, to set the number of times we want to repeat the data frame we can use rep.int,times argument. For example, if we have a data frame df and we want to create 5 replicates of df and add them in the original then sapply(df,rep.int,times=5) can be used.Examplesapply(df,rep.int,times=5)Output      x1          x2       x3    x4 [1,] 20.84538 9.486324 2.961236 967.9296 [2,] 23.29721 5.344792 3.044849 960.2204 [3,] 20.55978 6.064207 3.005293 1086.9639 [4,] 20.66044 8.436004 2.892010 1029.8222 [5,] 19.81347 9.277129 2.980567 1018.0453 [6,] 20.84538 9.486324 2.961236 967.9296 [7,] 23.29721 5.344792 3.044849 960.2204 [8,] 20.55978 6.064207 3.005293 1086.9639 [9,] 20.66044 8.436004 2.892010 1029.8222 [10,] 19.81347 9.277129 2.980567 1018.0453 [11,] 20.84538 9.486324 2.961236 967.9296 [12,] 23.29721 5.344792 3.044849 960.2204 [13,] 20.55978 6.064207 3.005293 1086.9639 [14,] 20.66044 8.436004 2.892010 1029.8222 [15,] 19.81347 9.277129 2.980567 1018.0453 [16,] 20.84538 9.486324 2.961236 967.9296 [17,] 23.29721 5.344792 3.044849 960.2204 [18,] 20.55978 6.064207 3.005293 1086.9639 [19,] 20.66044 8.436004 2.892010 1029.8222 [20,] 19.81347 9.277129 2.980567 1018.0453 [21,] 20.84538 9.486324 2.961236 967.9296 [22,] 23.29721 5.344792 3.044849 960.2204 [23,] 20.55978 6.064207 3.005293 1086.9639 [24,] 20.66044 8.436004 2.892010 1029.8222 [25,] 19.81347 9.277129 2.980567 1018.0453

Read More

How to align the bars of a barplot with the X-axis using ggplot2 in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 2K+ Views

The bar plot is created with geom_bar function but there always exist some space between the bars and the X-axis labels. If we want to reduce that space or completely remove it we need to use scale_y_continuous function by defining expand argument for former and scale_y_continuous(expand=c(0,0)) for latter.Exampleggplot(df,aes(x,y))+geom_bar(stat="identity")+scale_y_continuous(expand=c(0,0))Output

Read More

How to highlight text inside a plot created by ggplot2 using a box in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 288 Views

There might be many ways to highlight text inside a plot but the easiest one would be using geom_label function of ggplot2 package, with the help of this function we can put the required text and the aesthetics of that text by using a single line of code. It is highly recommended that we should use geom_label function with desired specifications.Examplelibrary(ggplot2) ggplot(df,aes(x))+geom_histogram(bins=30)+geom_label(aes(x=6,y=450,label="Normal Distribution"),fill="red")Output

Read More

How to subset a data frame by excluding a specific text value in an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 893 Views

To create a subset based on text value we can use rowSums function by defining the sums for the text equal to zero, this will help us to drop all the rows that contains that specific text value. For example, if we have a data frame df that contains A in many columns then all the rows of df excluding A can be selected as−df[rowSums(df=="A")==0, , drop=FALSE]Exampledf[rowSums(df=="E")==0, , drop=FALSE] Output x1 x2 x3 x4 x5 1 A  D  B  C C 2 B  D  D  D D 3 B  A  D  D D 5 C  D  C  C C 10 C ...

Read More

How to convert MANOVA data frame for two-dependent variables into a count table in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 280 Views

MANOVA refers to multivariate analysis of variance, in this method we have more than one dependent variable and multiple independent variables. We want to compare each level of the independent variable combination for each of the dependent variables. To convert MANOVA data frame for two-dependent variables into a count table, we can use cast function of reshape package but we need to melt the data frame first so that the casting can be done appropriately.ExampleID

Read More

How to view the complete output of tibble in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 1K+ Views

Tibbles are created when we analyze data using dplyr package and if the data size is large then only 10 values are printed in R. If we want to display the complete output of tibble then View function needs to be used. For example, if we want to perform calculation of counts then we should add View() at the end of the code with pipe operator.Exampledf%>%group_by(Group,Rating)%>%mutate(count=n())%>%View()Output

Read More

How to convert a data frame row into character vector in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 1K+ Views

To create a character vector in R we can enclose the vector values in double quotation marks but if we want to use a data frame row values to create a character vector then as.character function can be used. For example, if we have a data frame df then the values in first row of the df can form a character vector using as.character(df[1,]).ExampleG1

Read More

How to change the font size of legend in base R plot?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 3K+ Views

In base R, we can use legend function to add a legend to the plot. For example, if we want to create a histogram with legend on top-right position then we can use legend("topright",legend="Normal Distribution") and if we want to change the font size then we need to as cex argument as shown below:legend("topright",legend="Normal Distribution",cex=2)Examplelegend("topleft",legend="Histogram of",cex=1.5)Output

Read More

How to create scatterplot for factor levels in an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 636 Views

To create a scatterplot for factor levels, we can use facet_grid function of ggplot2 package. For example, suppose we have a factor column in a data frame df defined as F and numerical columns defined as x and y then the scatterplot for the factor levels can be created as −ggplot(df,aes(x,y))+geom_point()+facet_grid(~Factor)Examplelibrary(ggplot2) ggplot(df,aes(x,y))+geom_point()+facet_grid(~Factor)Output

Read More

How to create a sample from an R data frame if weights are assigned to the row values?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 1K+ Views

To create a random sample in R, we can use sample function but if the weight of the values is provided then we need to assign the probability of the values based on the weights. For example, if we have a data frame df that contains a column X with some values and another column Weight with the corresponding weights then a random sample of size 10 can be generated as follows −df[sample(seq_len(nrow(df)), 10, prob=df$Weight_x), ]Exampledf[sample(seq_len(nrow(df)), 5, prob=df$weight_x), ] Output x weight_x 11 5.257177 10 19 5.401021 9 13 5.334041 10 10 4.416107 6 5 6.593158 2Exampledf[sample(seq_len(nrow(df)), 3, prob=df$weight_x), ...

Read More
Showing 21–30 of 1,740 articles
« Prev 1 2 3 4 5 174 Next »
Advertisements