按其rowname和columnname访问值,而不是数字

问题描述 投票:2回答:2

我有一个有多个列和行的表。我想通过它的column namerowname访问每个值,并用这些值绘制一个图。

使用101 columns,表格如下所示:

IDs     Exam1  Exam2 Exam3 Exam4 .... Exam100
Ellie   12            48    33         64
Kate    98      34    21    76         
Joe     22      53    49               72        
Van     77            40    12
Xavier                      88         92

我想要的是能够达到给定的row(ID)的标记,并给予column(考试):

table[Ellie,Exam3] --> 48
table[Ellie,Exam100] --> 64
table[Ellie,Exam2] -->     (empty)

然后有了这些数字,我想看看Ellie如何将其他考试与Exam23100进行比较的分布。

我差点用R来弄清楚这一部分:

library(data.table)
library(ggplot2)
pdf("distirbution_given_row.pdf")
selectedvalues <- c(table[Ellie,Exam3] ,table[Ellie,Exam100]) 
library(plyr)
cdat <- ddply(selected values, "IDs", summarise, exams.mean=mean(exams))
selectedvaluesggplot <- ggplot(selectedvalues, aes(x=IDs, colour=exams)) + geom_density() + geom_vline(data=cdat, aes(xintercept=exams.mean, colour=IDs), linetype="dashed", size=1)
dev.off()

哪个应该为感兴趣的考试产生Ellie's标记与其他标记(如果它是一个空白,那么它不应该被视为零。它仍然是一个空白。)

Red: Marks for Exam3 and 100 and 2 , Blue: The marks for the rest 97 exams

红色:考试3,100和2的标记,蓝色:其余97个考试的标记(代码和图表是来自this link的ggplot2的一个例子。)

所有的想法都很感激!

r plot ggplot2 datatable
2个回答
0
投票

IIUC - 你想绘制每个ID选择考试与所有其他考试。请考虑以下步骤:

  1. 将数据重新整形为长格式,甚至根据需要将零替换为零。
  2. 通过ID运行qazxsw poi到子集数据并构建平均聚合数据和ggplots。
  3. by()中,在选择检查上创建SelectValues指标列,然后使用垂直线平均求和图形。

数据

by

重塑和图形

txt = 'IDs     Exam1  Exam2 Exam3 Exam4 Exam100
Ellie   12      NA      48    33         64
Kate    98      34      21    76         NA
Joe     22      53      49    NA         72        
Van     77      NA      40    12         NA
Xavier  NA      NA      NA    88         92'

exams_df <- read.table(text=txt, header = TRUE) 

# ADD OTHER EXAM COLUMNS (SEEDED FOR REPRODUCIBILITY)
set.seed(444)
exams_df[paste0("Exam", seq(5:99))] <- replicate(99-4, sample(100, 5))

产量

library(ggplot2) # ONLY PACKAGE NEEDED # FILL NA exams_df[is.na(exams_df)] <- 0 # RESHAPE (BASE R VERSION) exams_long_df <- reshape(exams_df, timevar = "Exam", times = names(exams_df)[grep("Exam", names(exams_df))], v.names = "Score", varying = names(exams_df)[grep("Exam", names(exams_df))], new.row.names = 1:1000, direction = "long") # GRAPH BY EACH ID by(exams_long_df, exams_long_df$IDs, FUN=function(df) { df$SelectValues <- ifelse(df$Exam %in% c("Exam1", "Exam3", "Exam100"), "Select Exams", "All Else") cdat <- aggregate(Score ~ SelectValues, df, FUN=mean) ggplot(df, aes(Score, colour=SelectValues)) + geom_density() + xlim(-50, 120) + labs(title=paste(df$IDs[[1]], "Density Plot of Scores"), x ="Exam Score", y = "Density") + geom_vline(data=cdat, aes(xintercept=Score, colour=SelectValues), linetype="dashed", size=1) })


2
投票

至少要访问您的数据,您可以执行以下操作:

enter image description here

现在我准备了一个随机创建数字的例子来说明你可以做些什么。首先让我们创建一个示例数据框

df=data.frame(IDs=c("Ellie","Kate","Joe","Van","Xavier"),Exam1=c(12,98,22,77,NA),Exam2=c(NA,34,53,NA,NA),
                  Exam3=c(48,21,49,40,NA),Exam4=c(33,76,NA,12,88))

row.names(df)=df$IDs

df=df%>%select(-IDs)

> df['Joe','Exam2']
[1] 53

要使用ggplot,建议将其转换为长格式:

df=as.data.frame(matrix(rnorm(505,50,10),ncol=101))
colnames(df)=c("IDs",paste0("Exam",as.character(1:100)))
df$IDs=c("Ellie","Kate","Joe","Van","Xavier")

从这里开始,您可以根据需要使用变量。例如,绘制每个ID的得分密度:

df0=df%>%gather(key="exams",value="score",-IDs)

或仅选择考试2,3,100并绘制不同考试的密度

ggplot(df0, aes(x=score,col=IDs)) + geom_density()
© www.soinside.com 2019 - 2024. All rights reserved.