Found 2038 Articles for R Programming

How to find the position of NA in an R vector?

Nizamuddin Siddiqui
Updated on 05-Feb-2021 08:20:00

1K+ Views

When we have NA’s/missing values in an R vector then we want to replace those NA’s and for this purpose we might be needing the position of those values. These positions will be helpful especially in situations when we want to manually replace the missing values. The replacement can be done by using which function with is.na.Example1 Live Demox1

How to check if a matrix is invertible or not in R?

Nizamuddin Siddiqui
Updated on 05-Feb-2021 08:15:19

840 Views

If the matrix is singular then it is not invertible and if it is non−singular then it is invertible. Therefore, we can check if a matrix is singular or not. We can use is.singular.matrix function of matrixcalc for this purpose. For example, if we have a matrix called M then to check whether it is invertible or not, we can use is.singular.matrix(M).Example1Loading matrixcalc package and creating a matrix −library(matrixcalc) M1

How to remove repeated column names in a data.table object in R?

Nizamuddin Siddiqui
Updated on 05-Feb-2021 08:00:04

303 Views

In data analysis, we sometimes deal with duplicated data or just the representation of the data with same name. One such situation is column names are same for two columns in a data.table object. For this purpose, we can make use of which function with the combination of duplicated function and set the output of that duplicate to NULL to remove the repeated column names.Example1Loading data.table package and creating a data.table object −library(data.table) x1

How to find the number of occurrences of unique and repeated characters in a string vector in R?

Nizamuddin Siddiqui
Updated on 05-Jan-2021 06:21:42

456 Views

To find the number of occurrences of unique characters in a string vector, we can use table function with the combination of rle and strsplit. For example, if we have a string vector x that contains some unique and repeated values then it can be created by using the below command −table(rle(strsplit(x, "")[[1]]))Example 1Live Demo> x1 x1Output[1] "ABDAJFSDAVCJDDAJFKDSAFKDSJKCJCCJCJDKD"Example> table(rle(strsplit(x1, "")[[1]]))Outputvalues lengths A B C D F J K S V 1 5 1 3 6 3 7 4 3 1 2 0 0 1 1 0 0 0 0 0It means A of length 1 occurred 5 times and A ... Read More

How to set the X-axis labels in histogram using ggplot2 at the center in R?

Nizamuddin Siddiqui
Updated on 05-Jan-2021 06:14:51

6K+ Views

The boundary argument of geom_histogram function and breaks argument of scale_x_continuous function can help us to set the X-axis labels in histogram using ggplot2 at the center. We need to be careful about choosing the boundary and breaks depending on the scale of the X-axis values. Check out the below example to understand how it works.ExampleConsider the below data frame −ExampleLive Demo> x df dfOutputx 1 5 2 7 3 6 4 4 5 7 6 7 7 10 8 3 9 6 10 6 11 5 12 4 13 4 14 6 15 7 16 4 17 1 18 ... Read More

How to create horizontal legend using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 05-Jan-2021 06:13:13

5K+ Views

The default legend direction is vertical but it can be changed to horizontal as well and for this purpose we can use legend.direction argument of theme function of ggplot2 package. For example, if we want to create a bar chart with x as categories and y as frequencies tjat are contained in a data frame df then the bar chart with horizontal legends for categories in x can be created as −ggplot(df, aes(x, y, fill=x))+geom_bar(stat="identity")+theme(legend.direction="horizontal")ExampleConsider the below data frame −Live Demo> x y df dfOutputx y 1 A 27 2 B 25 3 C 28Loading ggplot2 package and creating the ... Read More

How to remove names from a named vector in R?

Nizamuddin Siddiqui
Updated on 05-Jan-2021 06:10:12

6K+ Views

To assign names to the values of vector, we can use names function and the removal of names can be done by using unname function. For example, if we have a vector x that has elements with names and we want to remove the names of those elements then we can use the command unname(x).Example1Live Demo> x1 names(x1) x1OutputG K N V P F F A P D L N K J V H S L F C M F H T I V 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ... Read More

How to convert a string vector into title case in R?

Nizamuddin Siddiqui
Updated on 05-Jan-2021 06:08:33

503 Views

We cannot be sure about the data characteristics we get for analysis and mostly it is not well organised, thus, the first task would be to make it more organised. The string values not in title case should also be taken care of if it is especially supposed to be in title case. For this purpose, we can str_to_title function of stringr package.Example1Live Demo> x1 x1Output[1] "india" "united kingdom" "indonesia" "canada" [5] "canada" "india" "united kingdom" "canada" [9] "indonesia" "united kingdom" "indonesia" "canada" [13] "russia" "indonesia" "canada" "russia" [17] "united kingdom" "russia" "russia" "india" [21] "united kingdom" "india" "india" "united ... Read More

How to set a specific value for a range in an R vector?

Nizamuddin Siddiqui
Updated on 05-Jan-2021 06:05:46

287 Views

Suppose we have a vector that contains hundred values starting from 1 to 100 and we want to set values greater than 5 and less than 96 to 5 then it can be done with the help of ifelse function. For example, if such vector is named as x then the command will be as follows −ifelse(x>5 & x x1 x1Output[1] 2 4 1 6 7 4 0 1 6 4 0 7 1 3 3 1 4 6 7 7 0 2 7 3 9 4 4 8 6 3 3 5 4 5 6 5 6 [38] 2 ... Read More

How to find the number of positive values in an R vector?

Nizamuddin Siddiqui
Updated on 05-Jan-2021 06:04:20

2K+ Views

We know that positive values are greater than 0, therefore, we can use this condition with length function to find the number of positive values in a vector. For example, if we have a vector x that contains some positive and some negative values and we want to find the number of values that are positive then we can use the command length(x[x>0]).Example1Live Demo> x1 x1Output[1] 0.21314126 1.23449384 -1.02721325 -0.23168203 -1.36368881 -0.82416287 [7] 0.31224895 -0.90773340 0.10312288 -0.38914253 0.01196499 0.44875369 [13] 0.40820219 0.70172242 -0.23766272 -0.01023414 1.12403398 0.05837136 [19] -0.67403563 -0.26134292 0.31192384 -1.25116951 0.22115555 0.46544495 [25] 0.76567139 0.76948285 -1.42650924 0.24616899 0.18043015 1.04896235 ... Read More

Advertisements