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

时间:2019-04-24 13:11:47

标签: 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")

的想法是,对于列表中SP_name中不存在的每个元素,我想添加一行(因此,对于Sp6Sp7 filled by 0)并获得:

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
Sp6 0   0   0   0
Sp7 0   0   0   0

您对R有想法吗?

2 个答案:

答案 0 :(得分:1)

通过基础R的想法,即通过构建自定义函数来(本质上)处理行名

<form class="form-horizontal" method="post" action="/Home/Create">

    @Html.AntiForgeryToken()

    <fieldset>

        <!-- Form Name -->
        <legend>New Request</legend>

        <!-- Text input-->
        <div class="form-group">
            <label class="col-md-4 control-label" for="textinput">Summarize your issue in 100 characters</label>
            <div class="col-md-4">
                <input id="textinput" name="title" type="text" class="form-control input-md" required="" />
            </div>
        </div>

        <!-- Textarea -->
        <div class="form-group">
            <label class="col-md-4 control-label" for="textarea">Let us know about any additional information here</label>
            <div class="col-md-4">
                <textarea class="form-control" id="textarea1" name="description"></textarea>
            </div>
        </div>

        <!-- Button -->
        <div class="form-group">
            <label class="col-md-4 control-label" for="singlebutton"></label>
            <div class="col-md-4">
                <button>Submit Request</button>
            </div>
        </div>
    </fieldset>
    </form>

注意::如果您的[HttpPost] public ActionResult Create(string title, string description) 列是一个因素,则上述函数将引发(无害)警告。我的建议是将您的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 转换为字符(如果尚未转换)

答案 1 :(得分:0)

我们使用setdiff获得不在“ SP_names”列中的元素,将原始数据集与这些元素绑定,并将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

或使用add_row

library(tibble)
add_row(df1, SP_names = v1)

或使用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

或使用base R

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))