gpt4 book ai didi

r - 如何将第一列中的名称与重复行相关联?

转载 作者:行者123 更新时间:2023-12-04 10:53:13 28 4
gpt4 key购买 nike

我说过这个数据框:

gene0   1   2   3
gene1 0 0 5
gene2 6 8 0
gene3 5 5 5
0 0 5
1 2 3

我想将“未命名”列中的数字与基因相关联,以具有以下内容:
gene0   1   2   3
gene1 0 0 5
gene2 6 8 0
gene3 5 5 5
gene1 0 0 5
gene0 1 2 3

最好的方法是什么?我需要为此使用 linux 还是 R?

最佳答案

我们可以使用 match来自 base R

a1 <- do.call(paste, df1[-1])    
df1$V1 <- df1$V1[match(a1, unique(a1))]
df1$V1
#[1] "gene0" "gene1" "gene2" "gene3" "gene1" "gene0"

更新

使用 OP 的数据集
df1 <- read.csv("newest.csv", stringsAsFactors = FALSE)
df1$id[df1$id == ""] <- NA
a1 <- do.call(paste, df1[-1])
df1$id <- df1$id[match(a1, unique(a1))]
length(unique(df1$id))
#[1] 621

head(df1$id, 20)
#[1] "pop13_110" "pop1_2" "pop16_108" "pop2_10" "pop2_2" "pop2_3" "pop2_4" "pop2_5" "pop2_6" "pop2_7" "pop2_8"
#[12] "pop2_9" "pop2_10" "pop2_11" "pop7_81" "pop2_13" "pop2_15" "pop2_15" "pop2_16" "pop22_20"
tail(df1$id, 20)
# [1] "pop22_2" "pop22_3" "pop22_4" "pop22_5" "pop22_8" "pop22_9" "pop13_60" "pop16_131" "pop23_11" "pop22_25" "pop22"
#[12] "pop22_14" "pop22_15" "pop22_32" "pop22_28" "pop16_56" "pop22_18" "pop9_9" "pop22_21" "pop22_22"

数据
df1 <- structure(list(V1 = c("gene0", "gene1", "gene2", "gene3", NA, 
NA), V2 = c(1L, 0L, 6L, 5L, 0L, 1L), V3 = c(2L, 0L, 8L, 5L, 0L,
2L), V4 = c(3L, 5L, 0L, 5L, 5L, 3L)), class = "data.frame",
row.names = c(NA,
-6L))

关于r - 如何将第一列中的名称与重复行相关联?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59364229/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com