r - 对列应用函数,结果不会根据函数的结果替换列值

标签 r function dataframe dplyr conditional-statements

(这基本上是图片中的数据框)

structure(list(mz = c(40, 50, 60, 70, 80, 90), 
`sample in1` = c(10, 51, 125, 99, 675, 12), 
`sample in2` = c(9, 51, 125, 105, 2424, 5),
`Sample in3` = c(1, 51, 125, 300, 1241, 0.02), 
`blank 1` = c(5, 20, 50, 68, 0, 0),
`blank 2` = c(10, 20, 50, 77, 0, 0),
`blank 3` = c(15, 20, 50, 89, 0, 0.01)), 
row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"))

我有一个超过 50 列和 50,000 行的大型数据框,但这是我所拥有的数据框的简化版本。

enter image description here

我基本上需要做的是将“空白”列总结为一列。我已经成功地做到了这一点,在所有空白列上使用 RowMeans。 接下来,我想对数据框中的每个样本值应用一个函数,该函数是:样本值/该特定行的所有空白列的平均值 = Ratio SB。 棘手的部分是:我不希望这个函数的结果替换样本的列值。 我想要做的是 (1) 保持列值不变,如果 SB 比率大于设定限制(例如:比率 SB>2.5,应保持列值不变)。 OR (2) 如果 SB 比率的结果小于设定限制(例如:如果 SB 比率 <2.5,则针对该特定列值返回 0),则返回 0(或 NA)。 最后 (3),该函数不应在样本上运行(或保留样本值不变),以防数据帧中该行的平均空白值 = 0(如图 1 中倒数第二行的情况)

这些是我的代码的 3 个基本要素。 输出应该仍然是一个数据帧,当将其应用到图 1 所示的数据帧时,在运行我想要执行的函数时,数据帧中应该发生以下更改。 enter image description here

应用代码后应该是这样的:

structure(list(mz = c(40, 50, 60, 70, 80, 90), 
`sample in1` = c(NA, 51, NA, NA, 675, 12), 
`sample in2` = c(NA, 51, NA, NA, 2424, 5),
`Sample in3` = c(NA, 51, NA, 300, 1241, NA), 
`Blank Average` = c(10, 20, 50, 78, NA, 0.00333333),
row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"))

到目前为止,我尝试使用 case_when,但为此我需要恢复到列表结构,并且显然无法将其应用于数据帧。 我还尝试过:Ratio_X <-样本/平均空白数据帧[Ratio_x<2]<-0并尝试使用apply和map_dfc,但都没有成功。我可能做了一些非常错误的事情,如果有人对我有任何提示或解决方案,我会很高兴:)

如果有任何不清楚的地方,我深表歉意,我是 R 和堆栈溢出的完全初学者。如果您需要更多信息,请告诉我。预先感谢!

最佳答案

这是一个基本的 R 方法:

#Get all the columns which has 'blank' in it
blank_cols <- grep('blank', names(df))
#Get all the columns which has 'sample' in it. 
#Using `ignore.case` because you have "Sample" in 3rd column
sample_cols <- grep('sample', names(df), ignore.case = TRUE)
#Threshold limit to check for
thresh <- 2.5

#Get mean of blank_cols
df$Blank_avg <- rowMeans(df[, blank_cols], na.rm = TRUE)
#Compare sample_cols with the mean, replace them by NA if they are below thresh
df[sample_cols][sweep(df[sample_cols], 1, df1$Blank_avg, `/`) <= thresh] <- NA
#Turn Blank_avg to NA where Blank_avg = 0
df$Blank_avg[df$Blank_avg == 0] <- NA
#Remove blank_cols
result <- df[, -blank_cols]
result
# A tibble: 6 x 5
#     mz `sample in1` `sample in2` `Sample in3` Blank_avg
#  <dbl>        <dbl>        <dbl>        <dbl>     <dbl>
#1    40           NA           NA        NA     10      
#2    50           51           51        51     20      
#3    60           NA           NA        NA     50      
#4    70           NA           NA       300     78      
#5    80          675         2424      1241     NA      
#6    90           12            5       0.02   0.00333

关于r - 对列应用函数,结果不会根据函数的结果替换列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64037345/

相关文章:

r - 为什么 R 中的 allPerms 函数给出的组合总是少一种?

R:如何根据其他多个列查找一列中的不同值

javascript - 分解函数表达式 - 与几行/参数作斗争

javascript - 创建一个带有 2 个执行函数的按钮的模态,但我想将与返回 false 的函数绑定(bind)的按钮变灰

dataframe - Julia - dataframe - 如何在 by() 中使用字符串进行自定义输出列命名

python - 如果请求的索引位于整数索引标签之间,pandas 数据框中是否有内置方法来包含切片的下一个/上一个行?

r - 使用 RJSONIO 在 R 中解析深度嵌套的 JSON 结构

r - data.table 二级键的问题

python - 为什么这个while循环在if语句下不执行?

python - 如何将带有日期时间的 DataFrame 列拆分为两列 : one with dates and one with times of the day?