【问题标题】:Miscalculation after subsetting in RR中子集后的计算错误
【发布时间】:2017-05-04 15:43:51
【问题描述】:

我在对数据进行子集化时正确计算有问题。但是最初我从文件中提取一些信息到另一个文件。然后,我尝试计算每个器官的患者数量。以前正常工作的命令现在给了我一个错误。它没有显示任何错误 - 只是计算错误。

输入文件在这个链接:https://www.dropbox.com/sh/8bo4b4dpmydj19w/AADZ7WuoecrjPwm_qyF8NRMza?dl=0

这是我的命令行。

Clinical_Samples_map = read.xls("b.xlsx") # calling my file
Clinical_Samples_Original = read.xls("a.xlsx", sheet=1) # the file where I get additional information

Clinical_Samples_map$AnatomicLocation = Clinical_Samples_Original[match(Clinical_Samples_map$SampleID, Clinical_Samples_Original$TubeName),"AnatomicLocation"] 

map<-Clinical_Samples_map # Just changing the name

# Anatomic Location

sub_map_AnatomicLocation <- map[!duplicated(map$patient_number), ] # Excluding the duplicate of patient by checking patient_number column
sub_map_AnatomicLocation <- data.frame(sub_map_AnatomicLocation) 
sub_map_AnatomicLocation_patient <- subset(sub_map_AnatomicLocation, Disease != "Unknown" & AnatomicLocation != "Unknown") # Getting rid of "Unknown" value if there is any


AnatomicLocation_patient <- count_(sub_map_AnatomicLocation , c("Disease","AnatomicLocation"))

write.table(AnatomicLocation_patient, "AnatomicLocation_patient.txt",col.names = TRUE)
write.table(Clinical_Samples_map, "Clinical_Samples_map2.txt",col.names = TRUE)

但是,当我比较两个书面的 txt 文件时,我有不同的数字。有谁知道为什么会这样?例如,如果您查看 CD 回肠编号,它会显示 3 个患者,但是当我查看 Clinical_Samples_map2.txt 时,我可以数出 4 个。

如果我尝试用 ggplot 生成一些图,还有其他事情:

ggplot(data=Clinical_Samples_map, aes(x=Disease, y=AgeAtSampling, fill=Disease)) + 
  geom_boxplot(notch = TRUE) +  
  ggtitle("Clinical_Samples_map_Disease") +
  scale_y_continuous(name = "Age at Sampling", breaks = seq(0, 80, 20), limits=c(0, 80)) +
  scale_x_discrete(name = "Disease") + 
  geom_jitter(colour = "black", size = 2, width = 0.15, height = 0.3) + 
  theme(legend.position = "bottom") +
  labs(fill = "Disease") + 
  theme(axis.title=element_text(face="plain", size="30", color="black",family = "Gill Sans MT"), 
        axis.text.x = element_text(colour="grey20",size=20,angle=45,hjust=.5,vjust=.5,face="plain"), 
        axis.text.y = element_text(colour="grey20",size=20,angle=0,hjust=1,vjust=0,face="plain"), 
        legend.text=element_text(face="plain", size="30", color="black"),
        legend.title=element_text(face="plain", size="30", color="black"))

我收到一个错误:

错误:提供给连续刻度的离散值

我认为这就是问题所在。我可以克服这个来生成情节,但我不知道为什么它计算错误?

有人可以帮忙解决这个问题吗?我挣扎了这么久,还想不通。

非常感谢。

巴蒂

【问题讨论】:

  • 对不起我的错误...这里是使用的库: library(gdata) 和 library(dplyr)...
  • 请使用library 行更新您的帖子,而不是在 cmets 中,以便所有人都能看到。

标签: r ggplot2 count match subset


【解决方案1】:

我想我明白了。因为我从来没有遇到过这个问题,所以我从来没有想过。我只需要将某些列更改为字符

map[] <- lapply(map, as.character)

我首先使用了这个和我使用的特定列

map_AnatomicLocation$AgeAtSampling=as.numeric(levels(map_AnatomicLocation$AgeAtSampling))[map_AnatomicLocation$AgeAtSampling] # 如果离散值有问题

然后它让我可以绘图和计算。

【讨论】:

  • 这个解决方案解决了什么问题?您的计算错误或ggplot 错误?虽然我们鼓励您回答自己的问题,但请为未来的读者完整且一般地回答(几乎就像与过去的自己交谈)。
猜你喜欢
  • 1970-01-01
  • 2013-09-27
  • 1970-01-01
  • 2022-10-15
  • 2014-12-07
  • 1970-01-01
  • 2020-12-22
  • 1970-01-01
  • 2012-03-09
相关资源
最近更新 更多