简体   繁体   English

为过热热图创建数据帧时,dplyr 左连接错误

[英]dplyr left join error when create dataframe for superheat heatmap

I'm trying to prepare data for making a heatmap using the superheat package in R.我正在尝试使用 R 中的 superheat 包准备用于制作热图的数据。
I aim to use the left_join() to join two dataframes, one containing abundance data and one containing a column with corresponding site names.我的目标是使用left_join()连接两个数据left_join() ,一个包含丰度数据,另一个包含具有相应站点名称的列。 The superheat function does not accept my column with site names to be a factor.过热函数不接受我的带有站点名称的列作为一个因素。 I hoped left_join() would work around this problem.我希望left_join()可以解决这个问题。 So far my code des not work.到目前为止,我的代码 des 不起作用。 I would be grateful for your help!我将不胜感激您的帮助!

fishdiet <- read.csv("Capis_otu_superheat3_small.csv", header=TRUE, row.names = 1)
Sites <- read.csv("Sites.csv", header=TRUE)

# choose only columns with numeric values
mynumbers <- fishdiet[,c(2:15)]

# left join the 2 data frames 
joined.data <- left_join(data.frame(Sites = rownames(mynumbers)),
                           Sites,
                           by = "Site")
 # or try
joined.data <- left_join(Sites, mynumbers, by = "Site")

output: dput(head(fishdiet))输出:dput(head(fishdiet))

structure(list(Site = structure(c(3L, 6L, 5L, 3L, 4L, 7L), .Label = c("MLALR", 
"MLCCR", "MLPBL", "MLPPR", "MLPST", "MLRNW", "MLROL", "MLSCR", 
"MLSIS"), class = "factor"), A1 = c(0L, 0L, 0L, 0L, 0L, 0L), 
A2 = c(0L, 0L, 0L, 0L, 0L, 0L), A3 = c(0L, 0L, 2L, 0L, 0L, 
0L), A4 = c(3L, 0L, 5L, 0L, 52L, 9L), A5 = c(0L, 0L, 0L, 
0L, 0L, 0L), A6 = c(0L, 0L, 0L, 0L, 0L, 0L), A7 = c(0L, 0L, 
0L, 0L, 0L, 0L), A8 = c(0L, 0L, 0L, 0L, 0L, 0L), A9 = c(0L, 
0L, 0L, 0L, 0L, 0L), A10 = c(0L, 0L, 0L, 0L, 0L, 0L), A11 = c(0L, 
1757L, 0L, 0L, 0L, 716L), A12 = c(0L, 0L, 0L, 0L, 0L, 0L), 
A13 = c(24499L, 8785L, 7267L, 19885L, 69L, 12L), A14 = c(19L, 
0L, 0L, 0L, 0L, 0L)), row.names = c("BCS19-10-1_ML1926", 
"BCS19-10-2_ML1950", "BCS19-10-3_ML1974", "BCS19-10-4_ML1998", 
"BCS19-10-5_ML2022", "BCS19-10-6_ML2046"), class = "data.frame")

output: dput(head(Sites))输出:dput(head(Sites))

structure(list(Site = structure(c(3L, 6L, 5L, 3L, 4L, 7L), .Label = c("MLALR", 
"MLCCR", "MLPBL", "MLPPR", "MLPST", "MLRNW", "MLROL", "MLSCR", 
"MLSIS"), class = "factor")), row.names = c(NA, 6L), class = "data.frame")

It's a little unclear what you are aiming to achieve.有点不清楚你的目标是什么。 Both your attempts你的两次尝试

left_join(data.frame(Sites = rownames(mynumbers)), Sites, by = "Site")

and

left_join(Sites, mynumbers, by = "Site")

fail because mynumbers does not contain a column "Site" (which is what you are trying to join on).失败,因为mynumbers不包含“站点”列(这是您要加入的列)。 If your goal is to simply get rid of the factors you can just do如果你的目标是简单地摆脱你可以做的因素

fishdiet <- fishdiet %>%
    rownames_to_column() %>%  # necessary only if you want to preserve rownames
    mutate(fishdiet, Site = as.character(Site))

Then try to call your heatmap function on fishdiet .然后尝试在fishdiet上调用您的热图函数。 If it doesn't work, feel free to post your heatmap code and desired output, it will be easier to help you this way.如果它不起作用,请随时发布您的热图代码和所需的输出,这样可以更轻松地为您提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM