R 根据列表在数据框中添加额外的行

R 根据列表在数据框中添加额外的行,r,R,我有一个数据帧,比如 SP_names Gp1 Gp2 Gp3 Gp4 Sp1 0 0 1 1 Sp2 0 1 1 1 Sp3 1 1 2 3 Sp4 1 3 6 1 Sp5 0 2 0 2 以及一份清单,例如: list<-c("Sp1","Sp2","Sp3","Sp4","Sp5","Sp6","Sp7") 您对R有什么想法吗?我们使用setdiff获取不在“SP_名称”列中的元素,将原始数据集与这些元素绑定,并将

我有一个数据帧,比如

SP_names    Gp1 Gp2 Gp3 Gp4
Sp1 0   0   1   1
Sp2 0   1   1   1
Sp3 1   1   2   3
Sp4 1   3   6   1
Sp5 0   2   0   2
以及一份清单,例如:

list<-c("Sp1","Sp2","Sp3","Sp4","Sp5","Sp6","Sp7")

您对R有什么想法吗?

我们使用
setdiff
获取不在“SP_名称”列中的元素,将原始数据集与这些元素绑定,并将
NA
更改为0

library(dplyr)
v1 <- setdiff(list, df1$SP_names)
bind_rows(df1, tibble(SP_names = v1)) %>%
      mutate_if(is.numeric, replace_na, 0)
#   SP_names Gp1 Gp2 Gp3 Gp4
#1      Sp1   0   0   1   1
#2      Sp2   0   1   1   1
#3      Sp3   1   1   2   3
#4      Sp4   1   3   6   1
#5      Sp5   0   2   0   2
#6      Sp6   0   0   0   0
#7      Sp7   0   0   0   0

或使用
complete

library(tidyr)
df1 %>% 
  complete(SP_names = list, fill = list(Gp1 = 0, Gp2 = 0, Gp3 = 0, Gp4 = 0))
# A tibble: 7 x 5
#  SP_names   Gp1   Gp2   Gp3   Gp4
#  <chr>    <dbl> <dbl> <dbl> <dbl>
#1 Sp1          0     0     1     1
#2 Sp2          0     1     1     1
#3 Sp3          1     1     2     3
#4 Sp4          1     3     6     1
#5 Sp5          0     2     0     2
#6 Sp6          0     0     0     0
#7 Sp7          0     0     0     0
数据
df1a idea通过base R,通过构建一个自定义函数来(本质上)处理行名称

f1 <- function(df, list) {
    rownames(df) <- df$SP_names
    df[setdiff(list, df$SP_names),] <- 0
    df$SP_names <- rownames(df)
    rownames(df) <- NULL
    return(df)
}


f1(d2, list)
#  SP_names Gp1 Gp2 Gp3 Gp4
#1      Sp1   0   0   1   1
#2      Sp2   0   1   1   1
#3      Sp3   1   1   2   3
#4      Sp4   1   3   6   1
#5      Sp5   0   2   0   2
#6      Sp6   0   0   0   0
#7      Sp7   0   0   0   0

顺便说一句,你写了一个列表,但是用
c()
你创建了一个向量,它不是列表。
library(tidyr)
df1 %>% 
  complete(SP_names = list, fill = list(Gp1 = 0, Gp2 = 0, Gp3 = 0, Gp4 = 0))
# A tibble: 7 x 5
#  SP_names   Gp1   Gp2   Gp3   Gp4
#  <chr>    <dbl> <dbl> <dbl> <dbl>
#1 Sp1          0     0     1     1
#2 Sp2          0     1     1     1
#3 Sp3          1     1     2     3
#4 Sp4          1     3     6     1
#5 Sp5          0     2     0     2
#6 Sp6          0     0     0     0
#7 Sp7          0     0     0     0
out <- merge(df1, data.frame(SP_names = list), all = TRUE)
out[is.na(out)] <- 0
df1 <- structure(list(SP_names = c("Sp1", "Sp2", "Sp3", "Sp4", "Sp5"
), Gp1 = c(0L, 0L, 1L, 1L, 0L), Gp2 = c(0L, 1L, 1L, 3L, 2L), 
Gp3 = c(1L, 1L, 2L, 6L, 0L), Gp4 = c(1L, 1L, 3L, 1L, 2L)), 
class = "data.frame", row.names = c(NA, -5L))
f1 <- function(df, list) {
    rownames(df) <- df$SP_names
    df[setdiff(list, df$SP_names),] <- 0
    df$SP_names <- rownames(df)
    rownames(df) <- NULL
    return(df)
}


f1(d2, list)
#  SP_names Gp1 Gp2 Gp3 Gp4
#1      Sp1   0   0   1   1
#2      Sp2   0   1   1   1
#3      Sp3   1   1   2   3
#4      Sp4   1   3   6   1
#5      Sp5   0   2   0   2
#6      Sp6   0   0   0   0
#7      Sp7   0   0   0   0