Replace Missing Values with Linear Interpolation in R Vector

Nizamuddin Siddiqui
Updated on 19-Nov-2020 07:51:59

2K+ Views

The linear interpolation is a method of fitting a curve using linear polynomials and it helps us to create a new data points but these points lie within the range of the original values for which the linear interpolation is done. Sometimes these values may go a little far from the original values but not too far. In R, if we have some missing values then na.approx function of zoo package can be used to replace the NA with linear interpolation method.Example1Loading zoo package:Live Demo> library(zoo) > x1 x1Output[1] 2 2 2 5 2 2 5 NA 2 5Replacing NA ... Read More

Add a New Column to a Matrix in R

Nizamuddin Siddiqui
Updated on 19-Nov-2020 07:50:01

2K+ Views

A data collection process is one of the initial and very important tasks in a data analysis project and sometimes we miss something. Therefore, we need to collect that data later and add it to the originally collected data. This mistake can be done for matrix data as well, hence we might need to add a new column to original matrix and this can be done by using cbind function.Example1Live Demo> M1 M1Output [, 1] [, 2] [, 3] [, 4] [, 5] [1, ] 1 6 11 16 21 [2, ] 2 7 12 17 22 [3, ... Read More

Add Picture to Plot in Base R

Nizamuddin Siddiqui
Updated on 19-Nov-2020 07:44:05

5K+ Views

To add a picture to a plot in base R, we first need to read the picture in the appropriate format and then rasterImage function can be used. The most commonly used format for picture in R is PNG. A picture in PNG format can be added to a plot by supplying the values in the plot where we want to add the picture.ExampleLoading png package:> library(png)Reading png file:> Picture plot(1:10,ty="n")Output:Adding the picture in png file to the above plot:Example> rasterImage(Picture,3,3,7,7)Output:Example> plot(1:10,ty="n") > rasterImage(Picture,5,5,7,7)Output:

Find Sum Based on Categorical Variable in R Data Frame

Nizamuddin Siddiqui
Updated on 19-Nov-2020 07:41:20

2K+ Views

Finding group-wise mean is a common thing but if we go for step-by-step analysis then sum of values are also required when we have a categorical variable in our data set. This can be easily done with the help of group_by and summarise_each function of dplyr package.ExampleConsider the below data frame:Live Demo> Group Salary Emp EmpOutputGroup Salary 1 D 28256 2 B 31092 3 A 23147 4 C 28209 5 B 37676 6 C 33374 7 D 44864 8 B 40152 9 A 25843 10 A 40946 11 D 23321 12 A 42854 13 C 36960 14 A 35285 15 ... Read More

Find Mean of List Elements in R Without Unlisting

Nizamuddin Siddiqui
Updated on 19-Nov-2020 07:38:02

362 Views

Most of the times, unlisting is used to find the mean of list elements but we can also use double-square brackets for the same purpose. The double-square brackets are basically used to access the values in the elements of the list, thus mean function works with those values directly. Look at the below example to understand how it works.ExampleConsider the below list:Live Demo> x xOutput[1] 3 3 3 5 3 1 4 7 5 4 5 9 9 7 4 3 6 2 4 3 3 4 7 4 4 [26] 4 5 3 4 4 3 5 7 2 ... Read More

Extract Names of Vector Values from a Named Vector in R

Nizamuddin Siddiqui
Updated on 19-Nov-2020 07:32:32

5K+ Views

How to extract the names of vector values from a named vector in R?The names of vector values are created by using name function and the names can be extracted by using the same function. For example, if we have a vector called x that contains five values(1 to 5) and their names are defined as first, second, third, fourth and fifth then the names of values in x can be extracted by using names(x)[x==1].Example1Live Demo> x1 names(x1) x1Outputone two three four 1 2 3 4Example> names(x1)[x1==1]Output[1] "one"Example> names(x1)[x1==2]Output[1] "two" Example> names(x1)[x1==3]Output[1] "three"Example> names(x1)[x1==4]Output[1] "four" Example2Live Demo> x2 x2Output [1] ... Read More

Find Nth Term in Look and Say Sequence in Python

Arnab Chakraborty
Updated on 19-Nov-2020 07:28:45

676 Views

Suppose we have a number n we have to generate nth term in “Look and Say” sequence. This is a sequence whose few terms are like below −111211211111221The string will be read like1 (One)11 (One 1) So read the previous 1, and say “One 1”21 (Two 1) So read the previous 11, and say “Two 1”1211 (One 2 one 1) So read the previous 21, and say “One 2 one 1”111221 (One 1 one 2 two 1) So read the previous 1211, and say “One 1 one 2 two 1”Suppose we have a number n, 1

Perform Cartesian Join for Two Data Table Objects in R

Nizamuddin Siddiqui
Updated on 19-Nov-2020 07:27:49

1K+ Views

The cartesian join is the joining of two objects that creates the combination of each value in object with all the values in the other object. For example, if we have a vector x that contains 1, 2, 3 and the other object y contains a, b, c then the cartesian join will be 1a, 2a, 3a, 1b, 2b, 3b, 1c, 2c, and 3c. Check out the below examples to understand how it can be done.Example> library(data.table) > DT1 DT1Output x 1: 1 2: 2 3: 3 4: 4Example> DT2 DT2Output y 1: 25 2: ... Read More

Find Length of Longest Substring with Character Count of At Least K in Python

Arnab Chakraborty
Updated on 19-Nov-2020 07:26:55

280 Views

Suppose we have a string s where each characters are sorted and we also have a number k, we have to find the length of the longest substring such that every character occurs at least k times.So, if the input is like s = "aabccddeeffghij" k = 2, then the output will be 8, as the longest substring here is "ccddeeff" here every character occurs at least 2 times.To solve this, we will follow these steps −Define a function rc() . This will take lstc := a map with all characters and their occurrencesacc := a new listans := 0valid ... Read More

Find Column Number of Minimum Values in Each Row for a Data Frame in R

Nizamuddin Siddiqui
Updated on 19-Nov-2020 07:25:26

786 Views

To find the column number of minimum values in each row for a data frame, we can use apply function but if we want to return the output in tabular form then matrix function should be used. For example, if we have a data frame df then our problem can be solved by using the code: as.matrix(apply(df, 1, which.min)).ExampleConsider the below data frame:Live Demo> set.seed(37) > x1 x2 x3 x4 x5 df1 df1Outputx1 x2 x3 x4 x5 1 1 2 4 9 3 2 0 5 8 10 4 3 1 3 8 6 1 4 1 5 5 8 ... Read More

Advertisements