Found 2038 Articles for R Programming

How to remove a particular value from a vector in R?

Nizamuddin Siddiqui
Updated on 12-Nov-2021 08:37:14

1K+ Views

To remove a particular value from a vector, we can use negation of value.For example, if we have a vector called V that contains repetitive values starting from 1 to 10 and if we want to remove all 10s from V then we can use the command given below −V

How to find rows in an R data frame that do not have missing values?

Nizamuddin Siddiqui
Updated on 12-Nov-2021 08:27:59

407 Views

Dealing with missing values is one of the most critical task in data analysis. If we have a large amount of data then it is better to remove the rows that contains missing values. For the removal of such rows we can use complete.cases function.For example, if we have a data frame called df that contains some missing values then we can remove the rows with missing values using the below given command −df[complete.cases(df),]Example 1Following snippet creates a sample data frame −x1

How to highlight a value with underline in base R plot?

Nizamuddin Siddiqui
Updated on 12-Nov-2021 08:24:01

617 Views

To highlight a value with underline in base R plot, we can use underline function within text function. The text function will be used to define the value that we need to underline in the plot and underline function will only underline that part.Check out the below given example to understand how it works.ExampleTo highlight a value with underline in base R plot, use the code given below −plot(1:5)OutputIf you execute the above given code, it generates the following output −To highlight a value with underline in base R plot, add the following code to the above code −plot(1:5) text(4, ... Read More

How to find the last non-missing value in each column of a data.table object?

Nizamuddin Siddiqui
Updated on 12-Nov-2021 08:22:30

488 Views

To find the last non-missing value in each column of a data.table object, we can use lapply function along with tail function for NA values.For example, if we have a data.table object called DT then we can find the last non-missing value in each column of DT by using the below given command −DT[,lapply(.SD,function(x) tail(x[!is.na(x)],1))]Example 1Following snippet creates a data.table object −library(data.table) x1

Find the combination of columns for correlation coefficient greater than a certain value in R

Nizamuddin Siddiqui
Updated on 12-Nov-2021 08:13:33

530 Views

To find the combination of columns for correlation coefficient greater than a certain value, we would first need to create the correlation matrix and then melt the correlation with the help if melt function of reshape2 package. After that subset of the output will be taken based on the value of the correlation coefficient.Check out the below examples to understand how it works.Example 1Following snippet creates a sample data frame −x1

How to convert a text vector into a matrix in R?

Nizamuddin Siddiqui
Updated on 12-Nov-2021 08:15:37

605 Views

Sometimes numerical values are being read as strings and hence creating a matrix using that vector is not straight forward.For the conversion of text vector into a matrix, we need to use scan function and specify the appropriate number of rows or columns as shown in the below given examples.Example 1Consider the below given vector x1 −x1

How to create side by side barplot in base R?

Nizamuddin Siddiqui
Updated on 12-Nov-2021 08:05:05

2K+ Views

If we want to create side by side barplot for two vectors or two columns of same or different data frames then we first need to combine those vectors or columns with the help of cbind function as shown below. After that barplot function will be applied to the combined data and beside argument will be set to TRUE.Check out the below given example to understand how it works.ExampleTo create side by side barplot in base R, use the code given below −x

How to change the color of bars in base R barplot?

Nizamuddin Siddiqui
Updated on 12-Nov-2021 08:03:25

1K+ Views

To change the color of bars in base R barplot, we can use col argument inside the barplot function.For example, if we have a vector called V for which we want to create the barplot then we can use the command given below to get the bars in blue color − barplot(V,col="blue")Check out the below example to understand how it can be done.ExampleTo change the color of bars in base R barplot, use the code given below −x

How to create an alternately increasing sequence in R?

Nizamuddin Siddiqui
Updated on 12-Nov-2021 08:01:29

210 Views

To create an alternately increasing sequence, we can take help of logical constants in the R language that is TRUE and FALSE. While creating a vector, the first value will be set to FALSE and the second value will be set to TRUE, therefore, the resulting vector will always have an alternately increasing sequence.Check out the below given examples to understand how it works.Example 1To create an alternately increasing sequence in R, use the code given below −x1

How to find n number of quartiles for every row in R?

Nizamuddin Siddiqui
Updated on 12-Nov-2021 08:00:26

615 Views

To find the n number of quartiles for every row in an R data frame, we can use apply function along with quantile function.For example, if we have a data frame called df that contains hundred rows and we want to find two quartiles say first and third for each row then we can use the below mentioned command −apply(df,1,quantile,c(0.25,0.75))Example 1Following snippet creates a sample data frame −x1

Advertisements