- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a column with the serial number of values in character column of an R data frame?
A group column in an R data frame have duplicate values and we might want to create a column with the serial number based on the values such as first value of the first group gets 1, the same value gets 2 when occurred second time in the same column and so on. This can be done by using ave function as shown in the below examples.
Example
Consider the below data frame −
S.No<-1:20 Group<-sample(LETTERS[1:4],20,replace=TRUE) df1<-data.frame(S.No,Group) df1
Output
S.No Group 1 1 B 2 2 A 3 3 C 4 4 C 5 5 B 6 6 C 7 7 C 8 8 C 9 9 D 10 10 D 11 11 C 12 12 C 13 13 A 14 14 B 15 15 A 16 16 D 17 17 C 18 18 B 19 19 A 20 20 C
Creating a column for serial numbers of group values in df1 −
Example
df1$GroupWise_SerialNo<-with(df1,ave(as.character(Group),Group,FUN=seq_along)) df1
Output
S.No Group GroupWise_SerialNo 1 1 B 1 2 2 A 1 3 3 C 1 4 4 C 2 5 5 B 2 6 6 C 3 7 7 C 4 8 8 C 5 9 9 D 1 10 10 D 2 11 11 C 6 12 12 C 7 13 13 A 2 14 14 B 3 15 15 A 3 16 16 D 3 17 17 C 8 18 18 B 4 19 19 A 4 20 20 C 9
Example
ID<-1:20 Sample<-sample(c("India","China","UK"),20,replace=TRUE) df2<-data.frame(ID,Sample) df2
Output
ID Sample 1 1 India 2 2 China 3 3 China 4 4 UK 5 5 China 6 6 UK 7 7 India 8 8 India 9 9 UK 10 10 India 11 11 UK 12 12 India 13 13 UK 14 14 UK 15 15 UK 16 16 India 17 17 India 18 18 UK 19 19 UK 20 20 China
Creating a column for serial numbers of group values in df2 −
Example
df2$SampleWise_Numbering<- with(df2,ave(as.character(Sample),Sample,FUN=seq_along)) df2
Output
ID Sample SampleWise_Numbering 1 1 India 1 2 2 China 1 3 3 China 2 4 4 UK 1 5 5 China 3 6 6 UK 2 7 7 India 2 8 8 India 3 9 9 UK 3 10 10 India 4 11 11 UK 4 12 12 India 5 13 13 UK 5 14 14 UK 6 15 15 UK 7 16 16 India 6 17 17 India 7 18 18 UK 8 19 19 UK 9 20 20 China 4
- Related Articles
- How to concatenate column values and create a new column in an R data frame?
- How to replace missing values in a column with corresponding values in other column of an R data frame?
- How to remove a character in an R data frame column?
- Replace numerical column values based on character column values in R data frame.
- How to convert the character values in an R data frame column to lower case?
- How to create a boxplot of single column in R data frame with column name?
- How to find the number of non-empty values in an R data frame column?
- How to create a data frame with a column having repeated values in R?
- How to create a table of frequency for range of values in an R data frame column?
- How to create a column in an R data frame with cumulative sum?
- How to create a blank column with randomization in an R data frame?
- How to find the unique values in a column of an R data frame?
- How to find the sum of column values of an R data frame?
- How to create a group column in an R data frame?
- How to create a lagged column in an R data frame?

Advertisements