Found 2038 Articles for R Programming

How to extract string before slash from a vector in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 09:48:06

2K+ Views

If a vector contains string values and they are separated with a special character (This special character can be anything, also it is not necessarily to be a special character) and we want to extract only the values that exists before that special then we can use gsub function. For example, if we have a vector x that contain "UT/YT", "IST/IT", "PST/PT" then gsub can help us to extract UT, IST, PST.Examplex1

How to create histogram with relative frequency in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 09:46:49

1K+ Views

The relative frequency histogram can be created for the column of an R data frame or a vector that contains discrete data. For this purpose, we can use PlotRelativeFrequency function of HistogramTools package along with hist function to generate histogram. For example, if we have a vector x for which we want to create a histogram with relative frequencies then it can be done as PlotRelativeFrequency(hist(x)).ExampleConsider the below vector − Live Demox

How to find the cumulative sum for factor levels in an R data frame?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 09:42:55

438 Views

Cumulative sums are mostly used in descriptive analysis of data but sometimes we might want to calculate them in understanding the time series analysis for moving sums but it is very rare. If we have a factor column in an R data frame then it would not make sense to find the cumulative sum for all factor levels together, we must find the cumulative sums for each level. This can be easily done by using ave function.ExampleConsider the below data frame − Live Demoset.seed(15) x1

How to create a sequence of time in minutes with date in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 09:38:27

1K+ Views

To create a sequence of time in minutes with date we can use seq function and define the date and time with as.POSIXct. For example, if we want to generate a sequence of time between 2 pm on tenth October 2020 to 4 pm on the same date then we can use the following code −seq(from=as.POSIXct("2020-10-10 14:00"), to=as.POSIXct("2020-10-10 16:00"), by="min")Example Live Demoseq(from=as.POSIXct("2020-01-01 00:01"), to=as.POSIXct("2020-01-01 01:00"), by="min")Output[1] "2020-01-01 00:01:00 IST" "2020-01-01 00:02:00 IST" [3] "2020-01-01 00:03:00 IST" "2020-01-01 00:04:00 IST" [5] "2020-01-01 00:05:00 IST" "2020-01-01 00:06:00 IST" [7] "2020-01-01 00:07:00 IST" "2020-01-01 00:08:00 IST" [9] "2020-01-01 00:09:00 IST" "2020-01-01 00:10:00 IST" [11] ... Read More

How to omit missing values and move the values to places to complete the data frame structure in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 09:36:55

74 Views

When we have alternative missing values in two columns that makes the data frame look filled with values at alternate places in columns as well. In this case, we might want to remove those missing values so that the data frame becomes complete without any missing value. For this purpose we can use na.omit function with transform function as shown in the below examples.ExampleConsider the below data frame − Live Demox1

How to create regression model line in a scatterplot created by using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 09:33:46

246 Views

To add a regression model line in a scatterplot created by using ggplot2, we need to use geom_smooth function to define the line for linear model. For example, if we have a data frame df that contains independent variable x and the dependent variable y then the regression line can be created by using the code −ggplot(df,aes(x,y))+geom_point()+geom_smooth(method="lm")ExampleConsider the below data frame − Live Demoset.seed(133) x

How to create a plot with cross sign in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 09:31:03

265 Views

In base R, the plot with different shape of points can be created by using pch argument inside the plot function but if we want to use any sign that is not designed for pch argument by default then we should pass that particular sign. For example, if we want to use cross sign then we can use letter x with pch as pch = "x".ExampleConsider the below vector and create the plot by displaying cross sign using letter x − Live Demox

How to find the size of the plotting window in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 09:29:08

2K+ Views

The plotting window size can be found by using dev.size function and we can pass in for inches and cm for centimeters. For example, if we create a plot then we can use dev.size("in") to find the plot size in inches and dev.size("cm") to find the size in centimeters.ExampleConsider the below vectors and create a point chart between those vectors − Live Demox

Building an array from a string in JavaScript

AmitDiwan
Updated on 17-Oct-2020 09:16:03

65 Views

We have to write a function that creates an array with elements repeating from the string till the limit is reached.Suppose there is a string ‘aba’ and a limit 5.e.g. string = "string" and limit = 8 will give new arrayconst arr = ["s", "t", "r", "i", "n", “g”, “s”, ”t”]ExampleLet’s write the code for this function −const string = 'Hello'; const limit = 15; const createStringArray = (string, limit) => {    const arr = [];    for(let i = 0; i < limit; i++){       const index = i % string.length;       arr.push(string[index]);   ... Read More

How to convert columns of an R data frame into rows?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 09:26:48

20K+ Views

If the row values are incorrectly recorded into columns then we might want to convert the columns into rows. Thus, to convert columns of an R data frame into rows we can use transpose function t. For example, if we have a data frame df with five columns and five rows then we can convert the columns of the df into rows by using as.data.frame(t(df)).Example Live Demoset.seed(4) x1

Advertisements