Found 2038 Articles for R Programming

How to create a subset using character column with multiple matches in R?

Nizamuddin Siddiqui
Updated on 11-Feb-2021 12:02:55

557 Views

Subsetting is one of the most important aspects of data analysis. One such situation could be subsetting the character column based on multiple values. For example, if a character column of an R data frame has 5 categories then we might want to extract only 2 or 3 or 4 values then it can be done by using the filter function of dplyr package with str_detect function of stringr package.Consider the below data frame −Example Live DemoGroup

How to find the frequency vector elements that exists in another vector in R?

Nizamuddin Siddiqui
Updated on 11-Feb-2021 11:59:10

113 Views

If a vector value exists in another vector then we might want to find the frequency/count for such values in the other vector. For example, if we have two vectors say x and y, and some of the values in y exists in x as well. Therefore, we can find the frequency of values in x for y values can be found by using the command colSums(outer(x,y,"==")).Example Live Demox1

How to plot time series data with labels in R?

Nizamuddin Siddiqui
Updated on 11-Feb-2021 11:54:39

439 Views

If we have time series data stored in a data frame then plotting the same as a time series cannot be done directly, also the labels for the series might not be possible directly. Therefore, we first need to convert the data frame to a time series object by using the function ts as shown in the below example and then using the plot function to create the plot, this will display the labels for the series as well.Consider the below data frame −Example Live DemoTime

How to find the subtotal in R?

Nizamuddin Siddiqui
Updated on 11-Feb-2021 11:50:16

527 Views

By subtotal we mean finding the sum of values based on grouping column. For example, if we have a data frame called df that contains three numerical columns as x, y, z and one categorical column say Group then the subtotal of x, y, z for each category in Group can be found by using the command aggregate(cbind(x,y,z)~Group,data=df,FUN=sum).Consider the below data frame −Example Live Demox1

How to create a random vector of integers with increasing values only in R?

Nizamuddin Siddiqui
Updated on 11-Feb-2021 11:44:49

187 Views

To create a random vector of integers with increasing values, we can do random sampling with sample.int and for increasing values cummax function needs to be used. For example, to create a random vector of integers of size 5 up to values 5 starting from 1 can be done by using the command cummax(sample.int(5)).Example Live Demox1

How to get the list of available data frames in R environment?

Nizamuddin Siddiqui
Updated on 11-Feb-2021 11:42:02

2K+ Views

When we perform any type of data analysis, there are many types of objects that are created in the R environment such as vector, data frame, matrix, lists, arrays, etc. If we want to get the list of available data frames in R environment then we can use the below command −names(which(unlist(eapply(.GlobalEnv,is.data.frame))))Example Live Demox1

How to convert numeric columns to factor using dplyr package in R?

Nizamuddin Siddiqui
Updated on 11-Feb-2021 11:36:42

3K+ Views

If we have a numeric column in an R data frame and the unique number of values in the column is low that means the numerical column can be treated as a factor. Therefore, we can convert numeric columns to factor. To do this using dplyr package, we can use mutate_if function of dplyr package.Loading dplyr package and converting numerical columns in BOD data set (available in base R) to factor columns −Examplelibrary(dplyr) str(BOD) 'data.frame': 6 obs. of 2 variables: $ Time : num 1 2 3 4 5 7 $ demand: num 8.3 10.3 19 16 15.6 19.8 - ... Read More

How to create reverse of a number in R?

Nizamuddin Siddiqui
Updated on 11-Feb-2021 11:32:29

2K+ Views

To create reverse of a number, we can use stri_reverse function of stringi package. For example, if we have a vector called x that contain some numbers then the reverse of these numbers will be generated by using the command stri_reverse(x). But the output will be in character form, if we want to have numeric values then as.numeric function can be used.library(stringi)Example Live Demox1

How to create combination of multiple vectors in R?

Nizamuddin Siddiqui
Updated on 10-Feb-2021 11:21:59

3K+ Views

To create combination of multiple vectors, we can use expand.grid function. For example, if we have six vectors say x, y, z, a, b, and c then the combination of vectors can be created by using the command expand.grid(x,y,z,a,b,c).Example Live Demox1

How to check whether a particular word exists in an R data frame column?

Nizamuddin Siddiqui
Updated on 10-Feb-2021 11:14:09

659 Views

If we have a character column in an R data frame then we might want to check whether a particular value exist in the column or not. For example, if we have a gender column then we might want to check whether transgender exists in that column or not. This can be done with the help of grepl function. Check out the below examples to understand how it works.Consider the below data frame −Example Live Demox

Advertisements