如何从包含两个感兴趣关键字的行中检索值以在 R 上创建新列?

How do I retrieve a value from a row containing two keywords of interest to make a new column on R?

I have a data table that looks like this,

我基本上想做的是在 table 中创建一个新列,其中包含每个样本的 'readout' 中的倍数变化,例如,

样本 1 在第 0 周/样本 1 在第 0 周

第 4 周的样本 1/第 0 周的样本 1

第 14 周的样本 1/第 0 周的样本 1

等等,对于样本 1 的所有时间点,依此类推,然后使用第 0 周的各自 'readout' 为我的其余样本计算相同的东西。

到目前为止,我尝试过的是

r
SampleIDs<-as.character(unique(table$ID))

table$FC<-for(i in table[i,]){
for(j in SampleIDs){

if(table[i,"ID"]==j){

    table[i,3]/table[(("WEEK"==0)&("ID"==j)),3]
    }
    }
  }

}

当运行时,代码returns错误,

Error in if (table[i, "SampleID"] == j) { : argument is of length zero

我试图做的是创建一个具有唯一 ID 的单独向量,并在 for 函数中使用它逐行进行以确保该行包含具有相同 ID 的样本,然后尝试检索包含 ID 为 j AND 的样本数据的单元格来自第 0 周,以计算我的倍数变化值。任何有关如何执行此操作的帮助将不胜感激!谢谢

也许,我们可以按 'ID' 分组并通过将 'readout' 除以 'readout' 创建一个新列,其中 'WEEK' 是 0

library(dplyr)
df1 %>% 
    group_by(ID) %>% 
    mutate(new = readout/readout[WEEK == 0])

如果'WEEK'已经订购

df1 %>%
    group_by(ID) %>%
    mutate(new = readout/readout[1])

data.table

library(data.table)
setDT(df1)[, new := readout/readout[WEEK == 0], by = ID]

如果已经订购

setDT(df1)[, new := readout/readout[1], by = ID]

或使用base R

df1$new <- with(df1, readout/setNames(readout[WEEK == 0], unique(ID))[ID])

关于控制台显示+,只是一个符号,表示表达式不完整

这我们也可以在其他控制台中获得,例如在 Julia 中,REPL 不会显示任何符号,但会在完整表达式完成后给出输出

数据

df1 <- structure(list(ID = c(1L, 1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L, 3L), 
    WEEK = c(0, 4, 14, 24, 0, 4, 0, 4, 14, 24), readout = c(5, 
    6, 7, 8, 1, 1.5, 1, 1, 5, 3)), class = "data.frame", row.names = c(NA, 
-10L))

对于需要按组执行的操作,不应使用 for 循环。有一些函数可以帮助您执行此类分组计算。

如果 WEEK 尚未对数据进行排序,您可以先进行排序。

df <- df1[with(df1, order(ID, WEEK)),]

然后将 readout 除以每组中的第一个值。

这可以在 base R 中完成:

df$result <- with(df, readout/ave(readout, ID, FUN = function(x) x[1]))

dplyr

library(dplyr)
df %>% group_by(ID) %>% mutate(result = readout/first(readout))

data.table

library(data.table)
setDT(df)[, result := readout/first(readout), ID]