R Programming Articles

Page 18 of 174

How to create a date vector with randomization in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 283 Views

To create a date vector with randomization, we can use sample function but the dates need to be read with as.Date function. For example, if we have 2 dates say 2021-01-01 and 2021-02-02 then a vector with randomization of these three dates can be created by using the command −sample(c(as.Date("2021-01-01"),as.Date("2021-02-02")),100,replace=TRUE)Example1Date1

Read More

How to find the count of a particular character in a string vector in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 216 Views

To find the count of a particular character in a string vector we can use nchar function along with gsub. For example, if we have a vector called x that contains string such India, Russia, Indonesia then we can find the number of times character i occurred then we can use the command nchar(gsub("[^i]","",x)) and the output will be 1 1 1 because first I’s in India and Indonesia will not be considered as they are in uppercase.Example1x1

Read More

How to find the frequency table for factor columns in an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 834 Views

If we have factor columns in an R data frame then we want to find the frequency of each factor level for all the factor columns. This can be done with the help of sapply function with table function. For example, if we have a data frame called df that contains some factor columns then the frequency table for factor columns can be created by using the command sapply(df, table).Example1Consider the below data frame −> x1 x2 df1 df1Output   x1 x2 1  D  a 2  D  b 3  D  c 4  D  b 5  D  c 6  C  a 7 ...

Read More

How to find the row-wise mode of a matrix in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 715 Views

There is no in-built function to find the mode in R, hence we need to create one and then apply it to the rows of the matrix. The function for mode is created as follows −mode M1 M1Output     [,1] [,2] [,3] [,4] [,5] [1,]  2    2    1    2   2 [2,]  2    2    2    2   1 [3,]  2    2    1    1   1 [4,]  2    1    1    1   1 [5,]  2    1    1    2   2> apply(M1,1,mode)Output[1] 2 2 1 1 2Example2> M2 M2Output     [,1] [,2] [,3] [,4] [,5] [1,]  1    1    2    2    1 [2,]  2    1    1    2    1 [3,]  2    2    1    1    1 [4,]  2    1    1    2    2 [5,]  2    1    1    2    2 [6,]  1    2    1    1    2 [7,]  1    1    2    1    2 [8,]  2    2    1    2    1 [9,]  2    1    1    2    2 [10,] 1    1    2    2    2 [11,] 1    1    2    1    2 [12,] 1    2    2    2    1 [13,] 2    2    2    2    1 [14,] 2    1    2    2    1 [15,] 1    2    1    1    2 [16,] 2    2    1    2    1 [17,] 2    2    1    1    1 [18,] 2    1    1    2    1 [19,] 1    1    1    2    1 [20,] 2    1    1    2    2> apply(M2,1,mode)Output[1] 1 1 1 2 2 1 1 2 2 2 1 2 2 2 1 2 1 1 1 2Example3> M3 M3Output     [,1] [,2] [,3] [,4] [,5] [1,]  1    3    3    2    1 [2,]  2    3    1    2    2 [3,]  2    2    3    3    1 [4,]  1    3    1    3    2 [5,]  3    1    2    1    2 [6,]  2    3    1    1    1 [7,]  2    2    2    3    1 [8,]  1    2    2    2    2 [9,]  2    1    2    1    2 [10,] 1    3    1    2    1 [11,] 2    1    3    1    1 [12,] 1    1    3    2    2 [13,] 2    1    1    1    2 [14,] 2    1    3    3    2 [15,] 1    2    3    1    2 [16,] 1    2    1    2    1 [17,] 3    1    1    3    2 [18,] 3    3    3    3    1 [19,] 3    2    3    1    1 [20,] 3    3    2    2    1> apply(M3,1,mode)Output[1] 1 2 2 1 1 1 2 2 2 1 1 1 1 2 1 1 1 3 1 2Example4> M4 M4Output      [,1] [,2] [,3] [,4] [,5] [1,]  10    10   9    10   9 [2,]  9     9    10   9    9 [3,]  9     9    9    10   10 [4,]  10    9    9    10   10 [5,]  10    10   9    10   9 [6,]  10    10   9    10   10 [7,]  9     9    9    10   9 [8,]  9     10   9    10   9 [9,]  9     9    9    9    9 [10,] 9     10   9    10   9 [11,] 10    10   9    9    9 [12,] 9     9    9    9    9 [13,] 10    10   10   9 10 [14,] 10    9    10   10 10 [15,] 9     10   9    10 9 [16,] 9     10   9    10 9 [17,] 9     10   10   9 10 [18,] 9     9    9    9 10 [19,] 10    9    9    10 9 [20,] 10    9    9    10 9> apply(M4,1,mode)Output[1] 10 9 9 10 10 10 9 9 9 9 9 9 10 10 9 9 10 9 9 9

Read More

How to extract the first digit from a character column in an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 1K+ Views

If we have a character column in the data frame that contains string as well as numeric values and the first digit of the numeric values has some meaning that can help in data analysis then we can extract those first digits. For this purpose, we can use stri_extract_first function from stringi package.Example1Consider the below data frame −> x1 y1 df1 df1Output x1 y1 1 1 HT14L 2 2 HT14L 3 3 HT23L 4 4 HT14L 5 5 HT32L 6 6 HT32L 7 7 ...

Read More

How to convert an old data frame to new data frame in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 607 Views

To convert an old data frame to a new data frame, we can simply set the new name. For example, if we have a data frame called df and want to convert it to a new one let’s say df_new then it can be done as df_new x1 x2 df1 df1Output x1 x2 1 8 6 2 4 9 3 3 2 4 3 5 5 7 4 6 4 8 7 8 6 8 12 12 9 8 6 10 ...

Read More

How to change the position of missing values to the end of data frame in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 198 Views

Most of the times we need to deal with missing values in data science projects and these missing values can be occurred at any position. We might want to change the position of these missing values and send them to the end of the columns in the data frame. This can be done with the help of lapply function as shown in the below examples.Example1Consider the below data frame −> x1 x2 x3 df1 df1Output   x1 x2 x3 1  0  0  2 2  1  1  NA 3  1  NA 0 4  0  NA 2 5  1  NA 2 6  NA ...

Read More

How to remove rows in an R data frame column that has duplicate values greater than or equal to a certain number of times?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 396 Views

To remove rows from the data frame that duplicate values greater than a certain number of times, we can create a subset for rows having duplicate values less than the certain number of times. For this purpose, we first need to extract the rows and then subset the data frame with the particular column as shown in the below examples.Example1Consider the below data frame −> x1 x2 df1 df1Output   x1 x2 1  0  0 2  0  0 3  1  0 4  0  1 5  0  0 6  1  1 7  0  1 8  1  1 9  1  2 10 0 ...

Read More

How to find the number of numerical columns in an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 1K+ Views

We know that a data frame can contain any type of columns such as numerical, character, logical, factor, etc. And if a data frame contains multiple type of columns then we might want to find the number of columns for each type or of one type say numerical. For this purpose, we can use select_if function of dplyr package along with the length function as shown in the below examples.Example1Consider the below data frame −> x1 x2 x3 x4 df1 df1Output   x1      x2          x3    x4 1  a  -0.18404831  0.1082741 2 2  b  -0.28597330 ...

Read More

How to assign a column value in a data frame based on another column in another R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 5K+ Views

To assign a column value based on another column, we can use ifelse function. The ifelse function checks whether the value in one column of one data frame matches the value in another column of another data frame by using equal sign (==) and then replace the original value with the new column if there is no match else returns the original value. Check out the below example to understand how it can be done.ExampleConsider the below data frame −> x1 x2 df1 df1Output x1 x2 1 3 5 2 3 7 3 0 ...

Read More
Showing 171–180 of 1,740 articles
« Prev 1 16 17 18 19 20 174 Next »
Advertisements