How to append a sequential number for every element in a data frame?

安稳与你 提交于 2020-01-14 14:24:26

问题


This question is a follow-up to How to append column number in front of every element?

V1 <- c("a", "a", "b", "b", "b")
V2 <- c("c" ,"d", "e" ,"e", "f")
V3  <- c("i", "j", "k", "l", "m")
df <- data.frame(V1, V2, V3)
df[] <- Map(paste0, seq_along(df), df)

OUTPUT

  V1 V2 V3
1 1a 2c 3i
2 1a 2d 3j
3 1b 2e 3k
4 1b 2e 3l
5 1b 2f 3m

How can I create the following output instead?

  V1       V2       V3
1 1.1.a   2.1.c   3.1.i
2 1.1.a   2.2.d   3.2.j
3 1.2.b   2.3.e   3.3.k
4 1.2.b   2.3.e   3.4.l
5 1.2.b   2.4.f   3.5.m

回答1:


Another option is to use match with the unique elements of each of the column and the do the paste

df[] <- paste(col(df), sapply(df, function(x) match(x, unique(x))), 
                as.matrix(df), sep=".")
df
#     V1    V2    V3
#1 1.1.a 2.1.c 3.1.i
#2 1.1.a 2.2.d 3.2.j
#3 1.2.b 2.3.e 3.3.k
#4 1.2.b 2.3.e 3.4.l
#5 1.2.b 2.4.f 3.5.m

Or using tidyverse

library(tidyverse)
imap(seq_along(df), ~ 
           df %>% 
              select(.x) %>%
              mutate_at(1, funs(paste(.y, match(., unique(.)), ., sep="." )))) %>%
     bind_cols
#     V1    V2    V3
#1 1.1.a 2.1.c 3.1.i
#2 1.1.a 2.2.d 3.2.j
#3 1.2.b 2.3.e 3.3.k
#4 1.2.b 2.3.e 3.4.l
#5 1.2.b 2.4.f 3.5.m



回答2:


You can use the Map instruction preceded by a lapply.

f <- function(x){
  sp <- split(x, x)
  unlist(lapply(seq_along(sp), function(i) paste(i, sp[[i]], sep = ".")))
}

df[] <- lapply(df, f)
df[] <- Map(paste, seq_along(df), df, sep = ".")

df
#     V1    V2    V3
#1 1.1.a 2.1.c 3.1.i
#2 1.1.a 2.2.d 3.2.j
#3 1.2.b 2.3.e 3.3.k
#4 1.2.b 2.3.e 3.4.l
#5 1.2.b 2.4.f 3.5.m



回答3:


V1 <- c("a", "a", "b", "b", "b")
V2 <- c("c" ,"d", "e" ,"e", "f")
V3  <- c("i", "j", "k", "l", "m")
dtf <- data.frame(V1, V2, V3)

num <- sapply(dtf, function(x) cumsum(-duplicated(x) + 1))

(m <- sapply(1:3, function(x) paste(x, num[, x], dtf[, x], sep=".")))
#      [,1]    [,2]    [,3]   
# [1,] "1.1.a" "2.1.c" "3.1.i"
# [2,] "1.1.a" "2.2.d" "3.2.j"
# [3,] "1.2.b" "2.3.e" "3.3.k"
# [4,] "1.2.b" "2.3.e" "3.4.l"
# [5,] "1.2.b" "2.4.f" "3.5.m"

That's a matrix with no column names, but we can fix that.

as.data.frame(m, col.names=colnames(dtf))
#      V1    V2    V3
# 1 1.1.a 2.1.c 3.1.i
# 2 1.1.a 2.2.d 3.2.j
# 3 1.2.b 2.3.e 3.3.k
# 4 1.2.b 2.3.e 3.4.l
# 5 1.2.b 2.4.f 3.5.m


来源:https://stackoverflow.com/questions/52709564/how-to-append-a-sequential-number-for-every-element-in-a-data-frame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!