Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在列上应用函数,而不会根据函数的结果替换列值_R_Function_Dataframe_Dplyr_Conditional Statements - Fatal编程技术网

R 在列上应用函数,而不会根据函数的结果替换列值

R 在列上应用函数,而不会根据函数的结果替换列值,r,function,dataframe,dplyr,conditional-statements,R,Function,Dataframe,Dplyr,Conditional Statements,(这基本上是图片中的数据帧) 我有一个超过50列和50000行的大型数据框,但这是我的简化版本 我基本上需要做的是,将“空白”列总结为1列。我已经设法做到了,在所有空白列上使用RowMeans。 接下来,我想对数据帧中的每个样本值应用一个函数,函数是:特定行的样本值/所有空白列的平均值=比率SB。 棘手的部分是:我不希望此函数的结果替换示例的列值。 我想做的是,(1)保持列值不变,如果比率SB大于设置的限制(例如:比率SB>2.5,应保持列值不变)。或(2)如果SB比率的结果小于设定限值,则返

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

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

我基本上需要做的是,将“空白”列总结为1列。我已经设法做到了,在所有空白列上使用RowMeans。 接下来,我想对数据帧中的每个样本值应用一个函数,函数是:特定行的样本值/所有空白列的平均值=比率SB。 棘手的部分是:我不希望此函数的结果替换示例的列值。
我想做的是,(1)保持列值不变,如果比率SB大于设置的限制(例如:比率SB>2.5,应保持列值不变)。或(2)如果SB比率的结果小于设定限值,则返回0(或NA)(例如:如果比率SB,则为基本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
#获取所有包含“空白”的列

blank_cols图片对于绘图非常有用,但对于数据却毫无用处。您需要帮助编码,因此我们需要一些东西来测试和演示答案。请共享我们可以复制/粘贴到R中的样本数据-实现这一点的好方法是(a)共享代码以创建/模拟样本数据,或者(b)使用
dput()
为您已有的数据制作一个可复制/粘贴的版本。类似于
dput(您的_数据[1:5,c(“mz”,“blank1”,“blank2”)))
这三列中的5行。请显示小样本输入的预期输出,以便您的目标明确。谢谢!嗨,Akrun和Gregor,谢谢您的帮助!我甚至不知道dput(),我已经在原来的帖子中添加了代码。这足够了吗?或者你还需要更多信息吗?我基本上需要“SB ratio”函数,来检查所有列的值,看看它们的SB ratio值是否大于2.5(例如)。如果是这种情况,我不想更改列值,如果不是这种情况,我想用0或NA替换列值。请注意,如果“背景平均值”=0,列值也不应该更改(SB比率=样本/平均空白值=样本/0,这是不可能的).非常感谢!非常感谢,这是一个巨大的帮助!
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"))

#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