【问题标题】:How to plot specific data points in a column in R script如何在 R 脚本的列中绘制特定数据点
【发布时间】:2013-10-21 20:43:35
【问题描述】:

假设有两列,一列代表 p 值,另一列代表斜率。我想找到一种方法来仅绘制具有显着 p 值的斜率数据点。这是我的代码:

print("State the file name (include .csv)")
filename <- readline()
file <- read.csv(filename)

print ("Only include trials with p-value < .05? (enter yes or no)")
pval_filter <- readline()
if (pval_filter == "yes"){
   i <- 0
   count <- 0
   filtered <- NULL
   while (i > length(file$pval)){
      if (file$pval[i] < .05){
         filtered[count] <- i
         count <- count + 1
      }
      i <- i + 1
   }

   x <- 0
   while (x != -1){
      print("State the variable to be plotted")
      temp_var <- readline()
      counter <- 0
      var <- NULL
      while (counter > length(filtered)){
         var[counter] = file [, temp_var][filtered[counter]]
         counter <- counter + 1
         }

      print ("State the title of the histogram")
      title <- readline()
      hist(var, main = title, xlab = var)
      print("Enter -1 to exit or any other number to plot another variable")
      x <- readline()
    }
}

【问题讨论】:

  • 你有什么问题?

标签: r csv rscript


【解决方案1】:

这不是更短并且产生的结果大致相同:

df = read.csv('file.csv')
df = df[df$pval < 0.05,]
hist(df$value)

这至少应该让你开始。

关于代码的一些说明:

  • 您使用大量保留名称(var、file)作为对象名称,这是个坏主意。
  • 如果您希望程序使用用户输入,则需要在对其进行任何操作之前对其进行检查。
  • 无需显式循环遍历 data.frame 中的行,R 是矢量化的(例如,请参阅上面的 df 子集)。这种风格看起来像 Fortran,R 中不需要。

【讨论】:

    【解决方案2】:

    很难准确地说出你想要什么。最好是一个示例是可重现的(我们可以复制/粘贴并运行,我们没有您的数据,所以这不起作用)并且是最小的(您的代码中有很多我认为与您无关的问题)。

    但一些建议可能会有所帮助。

    首先,readline 函数有一个prompt 参数,与print 语句相比,它可以为您提供更好看的交互。

    如果您的所有数据都在一个数据框中,其中列 pb 用于 p 值和斜率,那么您可以仅包含 b 值,其中 p&lt;=0.05 具有简单的子集,例如:

    hist( mydataframe$b[ mydataframe$p <= 0.05 ] )
    

    with( mydataframe, hist(b[p<=0.05]) )
    

    这足以回答您的问题吗?

    【讨论】:

    • 是的。这与上面有效的答案非常相似。谢谢!
    【解决方案3】:

    鉴于data = cbind(slopes, pvalues)(所以col(data) == 2

    像这样:

    plot(data[data[ ,2] < 0.05 , ])
    

    解释:

    data[ ,2] &lt; 0.05 将返回一个带有列长度的 TRUE/FALSE 向量。

    那么你会得到:

    data[c(TRUE, FALSE....), ]  
    

    从那里开始,只有显示为 TRUE 的数据才会被选中。

    因此,您将只绘制 pvalue 低于 0.05 的那些 x 和 y。

    【讨论】:

      【解决方案4】:

      这是仅绘制具有显着 p 值的斜率数据点的代码: 假设文件的列名是 pval 和 slope。

      # Prompt a message on the Terminal
      filename <- readline("Enter the file name that have p-value and slopes (include .csv)")
      # Read the filename from the terminal
      file     <- read.csv(filename, header = TRUE)
      
      # Prompt a message again on the Terminal and read the acceptance from user
      pval_filter <- readline("Only include trials with p-value < .05? (enter yes or no)")    
      
      if (to-lower(pval_filter) == "yes"){
         # Create a filtered file that contain only rows with the p-val less than that of siginificatn p-val 0.05
         file.filtered <- file[file$pval < 0.05, ]    
      
         # Get the title of the Histogram to be drawn for the slopes (filtered)
         hist.title <- readline("State the title of the histogram")
         # Draw histogram for the slopes with the title
         #     las = 2 parameter in the histogram below makes the slopes to be written in parpendicular to the X-axis
         #     so that, the labels will not be overlapped, easily readable. 
         hist(file.filtered$slope, main = hist.title, xlab = Slope, ylab = frequency, las = 2)
      }
      

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-10-21
        • 2019-07-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-24
        • 2011-06-20
        相关资源
        最近更新 更多