Как добавить порядковый номер в столбец, если он зависит от значения предыдущего столбца? - PullRequest
0 голосов
/ 09 октября 2018

Это продолжение до Как добавить последовательный номер для каждого элемента в фрейме данных?

V1 <- c("a", "a", "b", "b", "a","c","b","c")
V2 <- c("e" ,"d", "f" ,"f", "d","d","e","f")

df <- data.frame(V1, V2)

df[] <- paste(col(df), sapply(df, function(x) match(x, unique(x))), as.matrix(df), sep=".")

ВЫХОД:

     V1    V2
1 1.1.a  2.1.e
2 1.1.a  2.2.d
3 1.2.b  2.3.f
4 1.2.b  2.3.f
5 1.1.a  2.2.d
6 1.3.c  2.2.d
7 1.2.b  2.1.e
8 1.3.c  2.3.f

Желаемый выход:

     V1    V2
1 1.1.a  2.1.e
2 1.1.a  2.2.d
3 1.2.b  2.3.f
4 1.2.b  2.3.f
5 1.1.a  2.2.d
6 1.3.c  2.4.d -- > the node number should be 2.4 (not 2.2) because V1 nodes in row 5 and 6 are different (1.1 vs 1.3)
7 1.2.b  2.5.e -- > the node number should be 2.5 (not 2.1) because V1 nodes in row 1 and 7 are different (1.1 vs 1.2)
8 1.3.c  2.6.f -- > the node number should be 2.6 (not 2.3) because V1 nodes in row 4 and 8 (1.2 vs 1.3)

1 Ответ

0 голосов
/ 09 октября 2018

Вот попытка, которая работает.Любопытно, если другие могут достичь более элегантно.

# Get a list of available node names
unused_nodes <- data_frame(V2 = paste0("2.", 1:100)) %>%
  anti_join(df %>% mutate(V2 = str_sub(V2,1,3))) %>%
  pull()

# Get a list of nodes that need to be renamed
dupes <- df %>%
  # collect unique combinations of V2 and V1
  distinct(V2, V1) %>%
  # For each V2, how many V1's was it tied to? Only keep dupes.
  group_by(V2) %>% mutate(version_of_V2 = row_number()) %>% ungroup() %>%
  filter(version_of_V2 > 1)
dupes$V2_new = paste0(unused_nodes[1:length(dupes$V1)],
                      str_sub(dupes$V2, -2))

# Bring in the renamed nodes and integrate into original
df %>%
  left_join(dupes) %>%
  mutate(V2 = if_else(!is.na(V2_new), V2_new, V2)) %>%
  select(-version_of_V2, -V2_new)

     V1    V2
1 1.1.a 2.1.e
2 1.1.a 2.2.d
3 1.2.b 2.3.f
4 1.2.b 2.3.f
5 1.1.a 2.2.d
6 1.3.c 2.4.d
7 1.2.b 2.5.e
8 1.3.c 2.6.f
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...