英文:
Combine Columns with same name in the same dataset in R
问题
例如:
我有一个数据框(df)
df
A | B | C | D | C |
---|---|---|---|---|
1 | 2 | 3 | 4 | 5 |
3 | 4 | 4 | 4 | 4 |
我想要得到这样的结果:
A | B | C | D |
---|---|---|---|
1 | 2 | 3 | 4 |
3 | 4 | 4 | 4 |
NA | NA | 5 | NA |
NA | NA | 4 | NA |
英文:
For example:
I have one df
df
A | B | C | D | C |
---|---|---|---|---|
1 | 2 | 3 | 4 | 5 |
3 | 4 | 4 | 4 | 4 |
I would like to have this:
A | B | C | D |
---|---|---|---|
1 | 2 | 3 | 4 |
3 | 4 | 4 | 4 |
NA | NA | 5 | NA |
NA | NA | 4 | NA |
答案1
得分: 1
一个基本的R选项:
l <- split.default(df, names(df)) %>|
lapply(\(x) unname(unlist(x)))
data.frame(sapply(l, "length<-", max(lengths(l))))
# A B C D
#1 1 2 3 4
#2 3 4 4 4
#3 NA NA 5 NA
#4 NA NA 4 NA
注意:代码部分没有进行翻译,只提供了代码的原文。
英文:
A base R option:
l <- split.default(df, names(df)) |>
lapply(\(x) unname(unlist(x)))
data.frame(sapply(l, "length<-", max(lengths(l))))
# A B C D
#1 1 2 3 4
#2 3 4 4 4
#3 NA NA 5 NA
#4 NA NA 4 NA
答案2
得分: 1
以下是代码部分的翻译:
library(tidyverse)
df <- tibble(A = c(1,3), B = c(2,4), C = c(3, 4), D = c(4, 4), C = c(5, 4), .name_repair = "minimal")
df
#> # A tibble: 2 × 5
#> A B C D C
#> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 1 2 3 4 5
#> 2 3 4 4 4 4
dupe_names <- duplicated(names(df))
bind_rows(df[!dupe_names], df[dupe_names])
#> # A tibble: 4 × 4
#> A B C D
#> <dbl> <dbl> <dbl> <dbl>
#> 1 1 2 3 4
#> 2 3 4 4 4
#> 3 NA NA 5 NA
#> 4 NA NA 4 NA
希望这对你有所帮助。
英文:
You might have done something wrong beforehand to get into this position so if you can try to fix issues upstream. Otherwise:
library(tidyverse)
df <- tibble(A = c(1,3), B = c(2,4), C = c(3, 4), D = c(4, 4), C = c(5, 4), .name_repair = "minimal")
df
#> # A tibble: 2 × 5
#> A B C D C
#> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 1 2 3 4 5
#> 2 3 4 4 4 4
dupe_names <- duplicated(names(df))
bind_rows(df[!dupe_names], df[dupe_names])
#> # A tibble: 4 × 4
#> A B C D
#> <dbl> <dbl> <dbl> <dbl>
#> 1 1 2 3 4
#> 2 3 4 4 4
#> 3 NA NA 5 NA
#> 4 NA NA 4 NA
答案3
得分: 1
使用 cbind.na
out <- do.call(qpcR:::cbind.na, lapply(split.default(df, sub("\\.\\d+", "", names(df))), unlist))
row.names(out) <- NULL
-输出
> out
A B C D
[1,] 1 2 3 4
[2,] 3 4 4 4
[3,] NA NA 5 NA
[4,] NA NA 4 NA
英文:
Using cbind.na
out <- do.call(qpcR:::cbind.na, lapply(split.default(df, sub("\\.\\d+", "",
names(df))), unlist))
row.names(out) <- NULL
-output
> out
A B C D
[1,] 1 2 3 4
[2,] 3 4 4 4
[3,] NA NA 5 NA
[4,] NA NA 4 NA
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论