Reputation: 39
I have a data frame with same character in specific rows:
a 1
a 3
a 7
b 4
b 8
I want to changed it:
a.1 1
a.2 3
a.3 7
b.1 4
b.2 8
Do you know any code in R for this?
Thanks a lot.
Upvotes: 0
Views: 62
Reputation: 887108
We can use dplyr/tidyr
. We group by 'V1', create a sequence column ('VN'), unite
the columns 'V1' and 'VN', and then rename
the column.
library(dplyr)
library(tidyr)
df %>%
group_by(V1) %>%
mutate(VN = row_number()) %>%
unite(V1n, V1, VN, sep='.') %>%
rename(V1=V1n)
# V1 V2
# (chr) (int)
#1 a.1 1
#2 a.2 3
#3 a.3 7
#4 b.1 4
#5 b.2 8
Upvotes: 0
Reputation: 31171
You can also use data.table
package:
library(data.table)
setDT(df)[,ix:=paste(V1,1:.N, sep='.'),V1][]
# V1 V2 ix
#1: a 1 a.1
#2: a 3 a.2
#3: a 7 a.3
#4: b 4 b.1
#5: b 8 b.2
Data:
df = structure(list(V1 = structure(c(1L, 1L, 1L, 2L, 2L), .Label = c("a",
"b"), class = "factor"), V2 = c(1L, 3L, 7L, 4L, 8L)), .Names = c("V1",
"V2"), class = "data.frame", row.names = c(NA, -5L))
Upvotes: 2
Reputation: 70266
In base R, you could do:
df$V1 <- with(df, paste(V1, ave(as.numeric(V1), V1, FUN = seq_along), sep="."))
print(df)
# V1 V2
#1 a.1 1
#2 a.2 3
#3 a.3 7
#4 b.1 4
#5 b.2 8
Upvotes: 1