简体   繁体   中英

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.
I aim to use the left_join() to join two dataframes, one containing abundance data and one containing a column with corresponding site names. The superheat function does not accept my column with site names to be a factor. I hoped left_join() would work around this problem. So far my code des not work. 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))

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))

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). 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 . If it doesn't work, feel free to post your heatmap code and desired output, it will be easier to help you this way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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