r - R函数替换数据集中的多个值
问题描述
我对 R 和一般编码非常陌生。我正在处理 HT-qPCR 数据,并且有数百个基因代码需要更改为基因名称。我正在使用包 plyr 中的函数 revalue 并且运行良好:
Ct1 <- revalue(Ct_data$Gene, c("AY1" = "16s"))
但是,由于我有数百个要重命名的值,我想知道有没有办法在所有样本的循环中执行此操作?我有一个带有相应基因名称的基因代码的 excel 文件,所以有人可以指出我如何使用这个 excel 文件重命名值的正确方向吗?
解决方案
如果您的替换已经在数据框中(将您的 Excel 文件读入 R),那么这是一个连接。像这样的东西:
# 1 read your excel file into R
library(readxl)
lookup = read_excel("path/to/your_excel.xlsx")
## I'll pretend the column names are `bad_name` and `good_name`
# 2 join to your current data
library(dplyr)
Ct_data = left_join(Ct_data, by = c("Gene" = "bad_name"))
# 3 OPTIONAL manually spot-check to make sure the good names are correct
View(Ct_data[c("Gene", "good_name")])
# 4 OPTIONAL if not all names are replaced, you may need to keep the original name in case `good_name` is missing
Ct_data = Ct_data %>%
mutate(good_name = coalesce(good_name, Gene))
# Drop the old column, keep the new
Ct_data = Ct_data %>%
select(-Gene) %>%
rename(Gen = good_name)
如果您需要更多帮助,请发布一个可重复的小示例,其中包含几行数据来说明剩余问题。dput()
是共享数据的最佳方式,因为它可以复制/粘贴并保留所有类和结构信息,例如dput(Ct_data[1:10, ])
前 10 行。选择要共享的相关子集。
推荐阅读
- string - 将字符序列转换为字符串的最有效方法(Scala/JVM)
- python - python http服务器2路通信
- codeigniter - 组的 Codeigniter 自定义元描述
- spring-data-rest - Spring DB 实体:构造函数、equals 和 hashcode
- javascript - 通过迭代动态创建的表单行和输入进行减法
- redhat-datavirt - teiid有没有md5之类的hash函数?
- python - 比较两个数据框的列而不合并数据框
- c# - 如何在.net的数据表列中放置空格和特殊字符
- r - 在 ggplot2 中使用交互式标签制作 ggplot Choropleth 地图
- salesforce - Apex 测试类 - 简单插入