Programming Articles - Page 1734 of 3366

How to match two string vectors if the strings case is different in both the vectors in R?

Nizamuddin Siddiqui
Updated on 04-Sep-2020 13:06:16

194 Views

We know that, R is a case sensitive programming language, hence matching strings of different case is not simple. For example, if a vector contains tutorialspoint and the other contains TUTORIALSPOINT then to check whether the strings match or not, we cannot use match function directly. To do this, we have to convert the lowercase string to uppercase or uppercase to lowercase with the match function.Examples Live Demo> x1 x1Output[1] "z" "v" "r" "y" "z" "l" "v" "t" "f" "p" "p" "z" "e" "b" "a" "o" "m" "d" [19] "e" "l" "y" "y" "u" "u" "w" "b" "a" "j" "n" "v" ... Read More

How to extract strings based on first character from a vector of strings in R?

Nizamuddin Siddiqui
Updated on 04-Sep-2020 11:20:54

384 Views

Sometimes a vector strings have patterns and sometimes we need to make patterns from a vector of strings based on the characters. For example, we might want to extract the states name of United States of America from a vector that contains all the names. This can be done by using grepl function.ExampleConsider the below vector containing states name in USA −> US_states US_states[grepl("^A", US_states)] [1] "Alabama" "Alaska" "American Samoa" "Arizona" [5] "Arkansas" > US_states[grepl("^B", US_states)] character(0) > US_states[grepl("^C", US_states)] [1] "California" "Colorado" "Connecticut" > US_states[grepl("^D", US_states)] [1] "Delaware" "District of Columbia" > US_states[grepl("^E", US_states)] character(0) > US_states[grepl("^F", US_states)] [1] ... Read More

How to find the difference of values of each row from previous by group in an R data frame?

Nizamuddin Siddiqui
Updated on 04-Sep-2020 11:11:23

1K+ Views

In Data Analysis, sometimes we need to find the difference of the current value from the previous value and it can be also needed for groups. It helps us to compare the differences among the values. In R, we can use dplyr package’s group_by and mutate function with lag.ExampleConsider the below data frame − Live Demo> Group Frequency df1 df1Output Group Frequency 1 A    7 2 A    6 3 A    9 4 A    12 5 B    19 6 B    19 7 B    4 8 B    6 9 C    14 10 C    6 ... Read More

How to find the number of columns of an R data frame that satisfies a condition based on row values?

Nizamuddin Siddiqui
Updated on 04-Sep-2020 11:07:15

873 Views

Sometimes we want to extract the count from the data frame and that count could be the number of columns that have same characteristics based on row values. For example, if we have a data frame containing three columns with fifty rows and the values are integers between 1 and 100 then we might want to find the number of columns that have value greater than 20 for each of the rows. This can be done by using rowSums function.ExampleConsider the below data frame − Live Demo> x1 x2 x3 df dfOutput x1 x2 x3 1 9 72 9 2 5 20 ... Read More

How to solve the simultaneous linear equations in R?

Nizamuddin Siddiqui
Updated on 04-Sep-2020 11:01:36

1K+ Views

The data in simultaneous equations can be read as matrix and then we can solve those matrices to find the value of the variables. For example, if we have three equations as −x + y + z = 6 3x + 2y + 4z = 9 2x + 2y – 6z = 3then we will convert these equations into matrices and solve them using solve function in R.Example1 Live Demo> A AOutput   [, 1] [, 2] [, 3] [1, ] 1    1    2 [2, ] 3    2    4 [3, ] 2    3    -6 Live Demo> b ... Read More

How to change the gridlines of Y-axis on a chart created by using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 04-Sep-2020 10:56:55

248 Views

Normally, the gridlines on a plot created by using ggplot2 package are a little far from each other but sometimes the plot looks better if the gridlines are close to each other, therefore, we might want to do so. This can be done by setting the minor_breaks and breaks using scale_y_continuous if the Y-axis plots a continuous variable.ExampleConsider the below data frame − Live Demo> x y df dfOutput   x  y 1 14 16 2 36 1 3 78 18 4 61 6 5 19 11 6 2 40 7 93 23 8 10 13 9 3 21 10 55 31 ... Read More

How to find standard deviations for all columns of an R data frame?

Nizamuddin Siddiqui
Updated on 04-Sep-2020 10:54:38

7K+ Views

To find the means of all columns in an R data frame, we can simply use colMeans function and it returns the mean. But for standard deviations, we do not have any direct function that can be used; therefore, we can use sd with apply and reference the columns to find the standard deviations for all column of an R data frame. For example, if we have a data frame df then the syntax using apply function to find the standard deviations for all columns will be apply(df, 2, sd), here 2 refers to the columns. If we want to ... Read More

How to change the row index after sampling an R data frame?

Nizamuddin Siddiqui
Updated on 04-Sep-2020 10:51:39

892 Views

When we take a random sample from an R data frame the sample rows have row numbers as in the original data frame, obviously it happens due to randomization. But it might create confusion while doing analysis, especially in cases when we need to use rows, therefore, we can convert the index number of rows to numbers from 1 to the number of rows in the selected sample.ExampleConsider the below data frame − Live Demo> set.seed(111) > x1 x2 x3 df1 df1Output      x1          x2       x3 1 1.735220712 2.8616625 1.824274 2 1.169264128 2.8469644 ... Read More

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

Nizamuddin Siddiqui
Updated on 04-Sep-2020 10:48:34

285 Views

A vector may have thousands of values and each of them could be different or same also. It is also possible that values can be grouped or randomly selected but having few similar values. Irrespective of the values in a vector, to find some largest values we need to sort the vector in ascending order then the largest values will be selected.Examples> x1 x1 [1] -1.4447473195 3.2906645299 -0.4680055849 0.1611487482 -0.7715094280 [6] 0.4442103640 0.3702444686 0.0783124252 1.3476432299 1.0140576107 [11] -0.0968917066 0.4628821017 0.3102594626 -0.2946001275 0.1498108166 [16] -0.6002154305 0.5905382364 1.3892651534 0.1008921325 -0.6486318692 [21] -0.0562831933 -0.6887431711 0.4907512082 -0.3994662410 0.7827897030 [26] 0.5294704584 -1.3802965730 -0.6159076490 -0.0009408529 1.6182294859 ... Read More

How to cut the elements of a numeric vector into multiple intervals in R?

Nizamuddin Siddiqui
Updated on 04-Sep-2020 10:45:49

484 Views

A numeric vector may contain a large number of elements; therefore, we might want to convert that vector into a vector of intervals. For example, if we have 1 to 10 values in a vector then we might want to convert that vector into a vector of intervals such as (1, 5) for 1, 2, 3, 4, and 5 and (6, 10) for 6, 7, 8, 9, 10). This can be done by using cut function where we will use breaks argument to combine the vector elements in an interval.Examples Live Demo> x1 x1Output[1] 1 2 3 4 5 6 7 ... Read More

Advertisements