Found 33676 Articles for Programming

How to find the number of NA’s in each column of an R data frame?

Nizamuddin Siddiqui
Updated on 09-Oct-2020 14:19:24

852 Views

Sometimes the data frame is filled with too many missing values/ NA’s and each column of the data frame contains at least one NA. In this case, we might want to find out how many missing values exists in each of the columns. Therefore, we can use colSums function along with is.na in the following manner: colSums(is.na(df)) #here df refers to data frame name.Consider the below data frame −Example Live Demoset.seed(109) x1

How to simulate normal distribution for a fixed limit in R?

Nizamuddin Siddiqui
Updated on 09-Oct-2020 13:26:24

661 Views

To simulate the normal distribution, we can use rnorm function in R but we cannot put a limit on the range of values for the simulation. If we want simulate this distribution for a fixed limit then truncnorm function of truncnorm package can be used. In this function, we can pass the limits with and without mean and standard deviation.Loading and installing truncnorm package −>install.packages("truncnorm") >library(truncnorm)Examplertruncnorm(n=10, a=0, b=10)[1] 0.76595522 0.33315633 1.29565988 0.67154230 0.04957334 0.38338705 [7] 0.75753005 0.65265304 0.63616552 0.45710877rtruncnorm(n=50, a=0, b=100)[1] 0.904997947 0.035692016 0.402963452 1.001102057 1.445190636 0.109245234 [7] 0.205630845 0.312428027 0.465876772 0.424647787 0.309222394 0.442172805 [13] 0.365503292 1.277570451 0.235747661 1.128447123 ... Read More

Sum of all the non-repeating elements of an array JavaScript

AmitDiwan
Updated on 09-Oct-2020 11:35:21

442 Views

Suppose, we have an array of numbers like this −const arr = [14, 54, 23, 14, 24, 33, 44, 54, 77, 87, 77, 14];We are required to write a JavaScript function that takes in one such array and counts the sum of all the elements of the array that appear only once in the array −For example:The output for the array mentioned above will be −356The code for this will be −const arr = [14, 54, 23, 14, 24, 33, 44, 54, 77, 87, 77, 14]; const nonRepeatingSum = arr => {    let res = 0;    for(let i = 0; i < arr.length; i++){       if(i !== arr.lastIndexOf(arr[i])){          continue;       };       res += arr[i];    };    return res; }; console.log(nonRepeatingSum(arr));Following is the output on console −30

How to create a transparent histogram using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 08-Oct-2020 15:23:20

3K+ Views

When we create a histogram using ggplot2 package, the area covered by the histogram is filled with grey color but we can remove that color to make the histogram look transparent. This can be done by using fill="transparent" and color="black" arguments in geom_histogram, we need to use color argument because if we don’t use then the borders of the histogram bars will also be removed and this color is not restricted to black color only.ExampleConsider the below data frame −set.seed(987) x

How to select values less than or greater than a specific percentile from an R data frame column?

Nizamuddin Siddiqui
Updated on 08-Oct-2020 15:21:21

673 Views

The percentiles divide a set of numeric values into hundred groups or individual values if the size of the values is 100. We can find percentiles for a numeric column of an R data frame, therefore, it is also possible to select values of a column based on these percentiles. For this purpose, we can use quantile function.ExampleConsider the below data frame −set.seed(111) x

How to convert integers into integers written in words in R?

Nizamuddin Siddiqui
Updated on 08-Oct-2020 15:17:25

95 Views

If we have numbers then we might want to convert those numbers into words. For example, converting 1 to one. This might be required in cases where we have text data and numbers are part of the text. Therefore, it would be better to represent the numbers in text form to make the uniformity in the text. This can be done by using replace_number function qdap package.Installing and loading qdap package−install.packages("qdap") library("qdap")Examplereplace_number("1") [1] "one" replace_number("10") [1] "ten" replace_number("100") [1] "one hundred" replace_number("1000") [1] "one thousand" replace_number("1001") [1] "one thousand one" replace_number("12000") [1] "twelve thousand" replace_number("12214") [1] "twelve thousand two hundred ... Read More

How to set NA values to TRUE for a Boolean column in an R data frame?

Nizamuddin Siddiqui
Updated on 08-Oct-2020 15:14:58

923 Views

Dealing with NA values is one of the boring and almost day to day task for an analyst and hence we need to replace it with the appropriate value. If in an R data frame, we have a Boolean column that represents TRUE and FALSE values, and we have only FALSE values then we might want to replace NA’s with TRUE. In this case, we can use single square bracket and is.na to set all NA’s to TRUE.Exampleset.seed(999) S.No.

How to fill the NA values from above row values in an R data frame?

Nizamuddin Siddiqui
Updated on 08-Oct-2020 15:06:47

903 Views

Sometimes we have missing values that can be replaced with the values on the above row values, it often happens in situations when the data is recorded manually and the person responsible for it just mention the unique values because he or she understand the data characteristics. But if this data needs to be re-used by someone else then it does not make sense and we have to connect with the concerned person. If the concerned person tells us that the first value in each row can be filled for every NA in the same column then it can be ... Read More

How to represent the mean with vertical line in a histogram created by hist function in R?

Nizamuddin Siddiqui
Updated on 08-Oct-2020 15:02:41

2K+ Views

The value of mean is an important characteristic of the data to be represented by a histogram, therefore, one might want to plot it with the histogram. If the histogram is created by using hist function then we can create a vertical line on the histogram with the help of abline function by defining mean of the data for vertical argument v.Exampleset.seed(101) x

How to find different elements between two string vectors in R?

Nizamuddin Siddiqui
Updated on 08-Oct-2020 15:00:01

1K+ Views

Just like numerical vectors, we can find the different elements between two string vectors if there exists any. For this purpose, we can use setdiff function. For example, if we have a vector V1 that contains a, b, c, d, e, f and the other vector V2 that contains a, e, h, k, l, p, r, u, v, w then the different elements between these two vectors can be found as setdiff(V1,V2).Example Live Demox1

Advertisements