Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Programming Articles
Page 17 of 2545
How to convert a data frame row into character vector in R?
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 MoreProgram to find contiguous intervals of a unique array in Python
Suppose we have a list of unique numbers called nums. We have to find a sorted 2D matrix of numbers where each list represents an inclusive interval summarizing number that are contiguous in nums.So, if the input is like nums = [10, 11, 12, 15, 16, 17, 28, 30], then the output will be [[10, 12], [15, 17], [28, 28], [30, 30]], as in the list [10 to 12], [15 to 17] are contiguous, and 28 and 30 are there, they are represented as [28 to 28] and [30 to 30].To solve this, we will follow these steps−sort the list ...
Read MoreHow to italicize boxplot label in R using ggplot2?
Like every other tool for statistical analysis R does not display the labels of a boxplot in italics, thus if we want to do this, we need to do it manually. In ggplot2, we have a function scale_x_discrete that can be used to change the default font to italic using expression function.Exampleggplot(df,aes(x,y))+geom_boxplot()+scale_x_discrete(labels=expression(italic(Female),italic(Male)))Output
Read MoreHow to change the font size of legend in base R plot?
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 MoreHow to replace missing values with median in an R data frame column?
To replace missing values with median, we can use the same trick that is used to replace missing values with mean. For example, if we have a data frame df that contain columns x and y where both of the columns contains some missing values then the missing values can be replaced with median as df$x[is.na(df$x)]
Read MoreHow to create scatterplot for factor levels in an R data frame?
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 MoreHow to change the Y axis limit for boxplot created by using ggplot2 in R?
One of the most important aspects of a boxplot is Y-axis labels because these labels help us to understand the limit of the variable. Since R generate these labels automatically in a good way, we stick with that but we can change that using coord_cartesian function with ylim as shown in the below example.Exampleggplot(df,aes(x,y))+geom_boxplot()+coord_cartesian(ylim=c(290,400))Output
Read MoreHow to create a sample from an R data frame if weights are assigned to the row values?
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 MoreHow to replicate whole data frame and add it in the original one in R?
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 MoreHow to align the bars of a barplot with the X-axis using ggplot2 in R?
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