【问题标题】:Create a Loop in R on participants; per participant 1 boxplot .tiff is created在参与者的 R 中创建一个循环;每个参与者创建 1 个箱线图 .tiff
【发布时间】:2019-12-08 19:02:27
【问题描述】:

尊敬的 Stack Overflow 社区,

我想使用不同的 .csv 文件(每个参与者 1 个)执行 4 个参与者的循环。 对于每个主题,我想生成一个条形图,在 x 轴上依赖“性别(男性,女性)”,在 y 轴上依赖“正确答案的比例”)。

这里显示了每个 .csv 文件在打开时的显示方式: 我想给因子 c("observernumber","trial","numstim","memo","sex","corResp","curresp","success") 加上名字

1   1   284 high    female  a   a   1
2   2   190 low     male    l   l   1
3   3   224 low     male    l   l   1
4   4   218 high    male    l   l   1
5   5   137 high    male    l   l   1
6   6   45  high    female  a   a   1
7   7   87  high    female  a   a   1
8   8   249 high    female  a   a   1
9   9   27  low     male    l   l   1
10  10  53  low     male    l   l   1
11  11  92  low     female  a   a   1
12  12  283 low     male    l   l   1

我首先为每个参与者提供 .csv 名称:

obsNames = c("1_gender","2_gender","3_gender","4_gender")
nsubj <- length(obsNames)

我预先分配了一个读取 obsNames 的矩阵:

allData = c() 
for (i in 1:obsNames) { 
  dat = read.csv(sprintf("%s_gender.csv", obsNames[i]), header = FALSE, sep = ";") 
  obsIndex = rep(i,nrow(dat)) 
  allData = rbind(allData, cbind(obsIndex,dat)) 
}

我列出了我拥有的所有因素:

names(allData) = c("subj", "trial", "sex", "success") 

我想为 4 个当前主题中的每一个绘制一个 .tiff 文件,该文件必须显示 a)b)c):

a) 当前观察者的名字作为标题

paste(obsNames[curSubj], ".tiff", sep = "")

b) x 轴上的“性别(男性,女性)”因子

"sex"

c)在 y 轴上带有“正确答案的比例”因子

"success"

我想为每个参与者制作一个箱线图,我应该如何完成这个脚本? 如何命名 x 和 y 值?:

for(curSubj in 1:nsubj){
  tiff(file = paste(obsNames[curSubj], ".tiff", sep = "")) 
  barCenters = barplot(
    main = paste(obsNames[curSubj],".tiff",sep = ""),
    yaxt = "n",
    ylab = "Proportion Correct",
    xlab = "Gender"
  ) 
  mgp.axis(labels = c("Female","Male")) 
  dev.off() 
}

使用有错误???

curSubj

【问题讨论】:

  • 您是否收到错误消息?为什么你说它不起作用?
  • 您可以简化并执行 for(curSubj in obsNames) 并从循环中删除 obsNames,但我认为这不是 amin 问题。
  • 你能把dput(allData)的输出贴在问题里吗,或者如果dput(head(allData, 20))的输出太大了,请?
  • 能否请您清理一下代码格式(例如添加缩进),以便您的代码更具可读性。还要删除与手头问题无关的任何内容(例如颜色,从您所说的来看,这不是您遇到的问题)。
  • 希望代码和问题在新的编辑中更加清晰

标签: r loops boxplot


【解决方案1】:

以下内容创建一个 TIFF 文件,其中每个 CSV 文件的名称与模式 "gender.csv" 匹配。
它使用ggplot2 图形。

首先,读入文件。

csv_files <- list.files(pattern = "gender\\.csv")
df_list <- lapply(csv_files, read.csv)
obsNames <- sub("\\.csv", "", csv_files)
df_list <- lapply(seq_along(df_list), function(i){
  Y <- cbind.data.frame(obsIndex = obsNames[i], df_list[[i]])
})

现在是条形图,保存在 TIFF 文件中。

library(ggplot2)
library(scales)

for(i in seq_along(df_list)){
  g <- ggplot(df_list[[i]], aes(x = sex)) +
    geom_bar(aes(y = (..count..)/sum(..count..)), 
             stat = "count", position = "dodge") +
    scale_y_continuous(labels = percent) +
    ylab(label = "Proportion of correct answer") +
    ggtitle(obsNames[i])

  outfile <- paste0(obsNames[i], ".tiff")
  tiff(filename = outfile)
  print(g)
  dev.off()
}

数据创建代码。

以下代码在您的工作目录中创建了 4 个 csv 文件,其中包含问题中发布的表结构。

f <- function(i, n){
  sex <- sample(c("male", "female"), n, TRUE)
  success <- rbinom(n, 1, prob = 0.5)
  df <- data.frame(trial = seq_len(n), sex, success)
  write.csv(df, paste0(i, "_gender.csv"), quote = FALSE, row.names = FALSE)
}

set.seed(1234)
n <- sample(20:50, 4)
lapply(1:4, function(i) f(i, n = n[i]))

【讨论】:

  • 非常感谢锐的帮助! =) !
  • 我在答案的开头添加了另一个问题,更改了 4 个 csv 中包含的列。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多