如何分隔相同名称下相同变量的两个值?
How to separate two values of same variable under same name?
我有一个这样的数据框:
library(tidyverse)
a <- tibble(x=c("mother","father","brother","brother"),y=c("a","b","c","d"))
b <- tibble(x=c("mother","father","brother","brother"),z=c("e","f","g","h"))
我想加入这些数据帧,以便每个 "brother" 只出现一次
我试过fulljoin
ab <- full_join(a,b,by="x")
并获得了这个:
# A tibble: 6 x 3
x y z
<chr> <chr> <chr>
1 mother a e
2 father b f
3 brother c g
4 brother c h
5 brother d g
6 brother d h
我需要的是这个:
ab <- tibble(x=c("mother","father","brother1","brother2"),y=c("a","b","c","d"),z=c("e","f","g","h"))
# A tibble: 4 x 3
x y z
<chr> <chr> <chr>
1 mother a e
2 father b f
3 brother1 c g
4 brother2 d h
不幸的是,第一个和第二个brother
彼此没有区别! R 怎么知道您想以这种方式加入他们,而不是相反?
我会尝试 "remove duplicates" 在原来的 data.frame
中添加“1”和“2”标识符。
我不懂 tidyverse 语法,但如果你重复的次数不超过两次,你可能想试试
a <- c("A", "B", "C", "C")
a[duplicated(a)] <- paste0(a[duplicated(a)], 2)
使用 dplyr 你可以做类似下面的事情,它添加了一个额外的变量 person
来识别 x
中每个组中的每个人,然后通过 x
和 person
:
library(dplyr)
a %>%
group_by(x) %>%
mutate(person = 1:n()) %>%
full_join(b %>%
group_by(x) %>%
mutate(person = 1:n()),
by = c("x", "person")
) %>%
select(x, person, y, z)
哪个returns:
# A tibble: 4 x 4
# Groups: x [3]
x person y z
<chr> <int> <chr> <chr>
1 mother 1 a e
2 father 1 b f
3 brother 1 c g
4 brother 2 d h
我有一个这样的数据框:
library(tidyverse)
a <- tibble(x=c("mother","father","brother","brother"),y=c("a","b","c","d"))
b <- tibble(x=c("mother","father","brother","brother"),z=c("e","f","g","h"))
我想加入这些数据帧,以便每个 "brother" 只出现一次
我试过fulljoin
ab <- full_join(a,b,by="x")
并获得了这个:
# A tibble: 6 x 3
x y z
<chr> <chr> <chr>
1 mother a e
2 father b f
3 brother c g
4 brother c h
5 brother d g
6 brother d h
我需要的是这个:
ab <- tibble(x=c("mother","father","brother1","brother2"),y=c("a","b","c","d"),z=c("e","f","g","h"))
# A tibble: 4 x 3
x y z
<chr> <chr> <chr>
1 mother a e
2 father b f
3 brother1 c g
4 brother2 d h
不幸的是,第一个和第二个brother
彼此没有区别! R 怎么知道您想以这种方式加入他们,而不是相反?
我会尝试 "remove duplicates" 在原来的 data.frame
中添加“1”和“2”标识符。
我不懂 tidyverse 语法,但如果你重复的次数不超过两次,你可能想试试
a <- c("A", "B", "C", "C")
a[duplicated(a)] <- paste0(a[duplicated(a)], 2)
使用 dplyr 你可以做类似下面的事情,它添加了一个额外的变量 person
来识别 x
中每个组中的每个人,然后通过 x
和 person
:
library(dplyr)
a %>%
group_by(x) %>%
mutate(person = 1:n()) %>%
full_join(b %>%
group_by(x) %>%
mutate(person = 1:n()),
by = c("x", "person")
) %>%
select(x, person, y, z)
哪个returns:
# A tibble: 4 x 4
# Groups: x [3]
x person y z
<chr> <int> <chr> <chr>
1 mother 1 a e
2 father 1 b f
3 brother 1 c g
4 brother 2 d h