Found 33676 Articles for Programming

How to generate a power sequence of two in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 12:15:49

6K+ Views

In R, for the calculation of power we can simply use power operator ^ and this will be also used in case of generating a power sequence. For example, if we want to generate a power sequence from 1 to 5 of 2 then we can use the code 2^(1:5) this will result 2 4 8 16 32.Example Live Demo2^(0:2)Output[1] 1 2 4Example Live Demo2^(0:10)Output[1] 1 2 4 8 16 32 64 128 256 512 1024 Example Live Demo2^(0:50)Output[1] 1.000000e+00 2.000000e+00 4.000000e+00 8.000000e+00 1.600000e+01 [6] 3.200000e+01 6.400000e+01 1.280000e+02 2.560000e+02 5.120000e+02 [11] 1.024000e+03 2.048000e+03 4.096000e+03 8.192000e+03 1.638400e+04 [16] 3.276800e+04 6.553600e+04 1.310720e+05 2.621440e+05 5.242880e+05 [21] ... Read More

How to convert more than one column in R data frame to from integer to numeric in a single line code?

Nizamuddin Siddiqui
Updated on 08-Sep-2023 23:06:52

49K+ Views

To convert columns of an R data frame from integer to numeric we can use lapply() function. For example, if we have a data frame df that contains all integer columns then we can use the code lapply(df,as.numeric) to convert all of the columns data type into numeric data type.Example1Consider the below data frame − Live Demoset.seed(871) x1

How to create a point chart with empty points using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 12:09:22

3K+ Views

The point chart can be created by using geom_point function and if we want to create a point chart for single vector then we should pass the vector in both the places inside aes function. Also, by default the points are complete black circles and if we want to change the points to empty points then shape argument can be used.ExampleConsider the below data frame − Live Demoset.seed(171) x

How to generate random samples rounded to 4 decimal places in R?

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

1K+ Views

Random samples can be generated in many ways such as using discrete and continuous distributions, using integer vectors, using numerical vectors, using character vectors and/or factor vectors, also with columns of a data set. If we have the sample that is continuous in nature then the values are likely to contain many values after decimal point and we can limit those values to 4 or use any other limit using round function.Example Live Demox1

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

2K+ 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

614 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

117 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

335 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

Advertisements