R 如何将数据帧转换为二进制数(1和0)?
所以我有一些基因表达计数数据,列中包含我所有的样本,每行包含约60000个基因。我已经将数据转换为TPM,我想排除TPM阈值5以下的某些基因。为此,我需要使用R 如何将数据帧转换为二进制数(1和0)?,r,R,所以我有一些基因表达计数数据,列中包含我所有的样本,每行包含约60000个基因。我已经将数据转换为TPM,我想排除TPM阈值5以下的某些基因。为此,我需要使用df[df>5]将TPM值>5转换为1。这是否有效: data.frame(cbind(df[1],apply(df[-1],2,function(x) +(x > 5)))) gene SampleA SampleB 1 Gene 1 1 1 2 Gene 2 0 1 3 Ge
df[df>5]将TPM值>5转换为1。这是否有效:
data.frame(cbind(df[1],apply(df[-1],2,function(x) +(x > 5))))
gene SampleA SampleB
1 Gene 1 1 1
2 Gene 2 0 1
3 Gene 3 1 0
一个整洁的选择
library(dplyr)
mutate(df, across(-Gene, ~ + (. > 5)))
# # A tibble: 3 x 3
# Gene `Sample A` `Sample B`
# <chr> <int> <int>
# 1 Gene 1 1 1
# 2 Gene 2 0 1
# 3 Gene 3 1 0
库(dplyr)
突变(df,跨(-Gene,~+(.>5)))
##tibble:3 x 3
#基因`样本A``样本B`
#
#1基因1
#2基因2 0 1
#3基因3110
资料
df您能分享一下输入表的例子吗?但是根据你的描述,我认为一个简单的ifelse
可以工作吗df$column\u name=ifelse(df$column\u name>5,1,ifelse(df$column\u name<5,0,df$column\u name)
这很有效,谢谢
library(dplyr)
mutate(df, across(-Gene, ~ + (. > 5)))
# # A tibble: 3 x 3
# Gene `Sample A` `Sample B`
# <chr> <int> <int>
# 1 Gene 1 1 1
# 2 Gene 2 0 1
# 3 Gene 3 1 0
df <- structure(list(Gene = c("Gene 1", "Gene 2", "Gene 3"), `Sample A` = c(10.23,
2.89, 9.66), `Sample B` = c(11.2, 6.76, 4.34)), row.names = c(NA,
-3L), class = c("tbl_df", "tbl", "data.frame"))