Found 2038 Articles for R Programming

How to hide NA values in an R matrix?

Nizamuddin Siddiqui
Updated on 10-Feb-2021 06:04:18

182 Views

The hiding of NA values does not mean removal of NA values. If we have some NA values in an R matrix then we can hide them using blanks with double quotes. For example, suppose we have a matrix called M that contains some NA values then M can be printed by hiding NA value using print.table(M,na.print="")Example Live DemoM1

How to fix the lower value for X-axis in base R

Nizamuddin Siddiqui
Updated on 10-Feb-2021 05:51:47

264 Views

To fix the lower value for X-axis in base R, we can use xlim argument in plot function. Mostly, we set the lower value to zero but it is not necessary it can be something else, either less than zero or greater than zero as well. If only lower value needs to be fixed then upper value will be set by using the max function as shown in the below example.Examplex

How to save a csv and read using fread in R?

Nizamuddin Siddiqui
Updated on 10-Feb-2021 05:52:56

554 Views

To save a csv file, we can use write.csv function in R and if we want to read it using fread then fread function will be used with the name of the csv file. The benefit of reading the csv file with fread function is that there will be a variable added to the original csv file which contains the id as integers starting from 1 to the length of column values.Consider the below data frame −Example Live Demox

How to create a boxplot with log of the variable in base R?

Nizamuddin Siddiqui
Updated on 10-Feb-2021 05:31:37

2K+ Views

To create a boxplot with log of the variable in base R, we need to use log argument within the boxplot function but we need to carefully pass the Y-axis inside the function because the values of the boxplot are plotted on the Y-axis. For example, if we have a vector called x then the boxplot of log of x will be created as boxplot(x,log="y").Examplex

How to plot matrix columns as lines in base R?

Nizamuddin Siddiqui
Updated on 10-Feb-2021 05:29:45

1K+ Views

To plot matrix columns as lines in base R, we can use matplot function but we need to read the matrix as a data frame using as.data.frame function and for creating lines the type argument will be used. For example, if we have a matrix called M then the columns of M can be plotted as lines using the command matplot(as.data.frame(M),type="l").Consider the below data frame −Example Live DemoM

How to add one column in an R data frame to rest of the next columns?

Nizamuddin Siddiqui
Updated on 10-Feb-2021 05:20:45

122 Views

Data Analysis projects might require things that we generally think simple but they are actually helpful to achieve objectives, one such thing would be adding values or columns of an R data frame. To add one column to rest of the next columns, we can replace the next columns by adding the one column by using single square brackets.Consider the below data frame −Example Live Demox1

How to export data frame in R to excel?

Nizamuddin Siddiqui
Updated on 10-Feb-2021 05:04:53

535 Views

To export data frame in R to excel can be done with the help of write.xlsx function of xlsx package. We would need to pass the data frame name, the file name and the sheet name in which we want to save the data frame. For example, if we have a data frame called df with file name Excel_df and on sheet1 then the data frame will be saved by using the below command −write.xlsx(df, file="Excel_df.xlsx", sheetName = "Sheet1")Consider the below data frame −Example Live Demox

How to find mean and standard deviation from frequency table in R?

Nizamuddin Siddiqui
Updated on 09-Feb-2021 12:36:12

4K+ Views

To find the mean and standard deviation from frequency table, we would need to apply the formula for mean and standard deviation for frequency data. For example, if we have a data frame called df that contains a column x for units and frequency for counts then the mean and standard deviation can be calculated as −Mean = sum(df$x*df$frequency)/sum(df$frequency) SD = sqrt(sum((df$x−Mean)**2*df$frequency)/(sum(df$frequency)−1)) respectively.Example1 Live Demox

How to find unique matrices in a list in R?

Nizamuddin Siddiqui
Updated on 09-Feb-2021 12:33:23

193 Views

A list can contain many types of elements such as vectors, matrices, data frames etc. If we have matrices in a list then to find unique matrices in that list, we can simply use unique function. For example, if we have a list called LIST that contains matrices having some duplicate matrices then unique matrices can be extracted by using unique(LIST).Example1 Live Demolist(M1=matrix(1:25, ncol=5), M2=matrix(1:25, ncol=5), M3=matrix(1:25, ncol=5), M4=matrix(rpois(25, 5), ncol=5)) List1Output$M1 [, 1] [, 2] [, 3] [, 4] [, 5] [1, ] 1 6 11 16 21 [2, ] 2 7 12 17 22 [3, ] 3 8 13 ... Read More

How to return the same index for consecutively duplicated values in an R vector?

Nizamuddin Siddiqui
Updated on 09-Feb-2021 12:33:07

91 Views

It is obvious that duplicate values in an R vector do not have same indexes but we might want to create the same index for consecutively duplicated values, this will help to recognize the groups of duplicated values. For this purpose, we can use cumsum function along with diff function as shown in the below examples.Example1 Live Demox1

Advertisements