想象一下有两列,一列是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()
}
}
答案 0 :(得分:4)
这不是更短,产生的大致相同:
df = read.csv('file.csv')
df = df[df$pval < 0.05,]
hist(df$value)
这至少应该让你开始。
关于代码的一些评论:
df
)。这种风格看起来像Fortran,在R.中没有必要。答案 1 :(得分:2)
很难准确说出你想要的东西。最好是一个例子是可重现的(我们可以复制/粘贴和运行,我们没有你的数据,因此不起作用)并且是最小的(你的代码中有很多我不认为与你的交易问题)。
但有些指示可能会有所帮助。
首先,readline
函数有一个prompt
参数,可以提供比print
语句更好看的交互。
如果您的所有数据都位于包含p
列和b
列的p值和斜率的数据框中,那么您只能包含b
p<=0.05
的值hist( mydataframe$b[ mydataframe$p <= 0.05 ] )
使用简单的子集,如:
with( mydataframe, hist(b[p<=0.05]) )
或
{{1}}
这足以回答你的问题吗?
答案 2 :(得分:1)
鉴于data = cbind(slopes, pvalues)
(所以col(data) == 2
)
像这样:
plot(data[data[ ,2] < 0.05 , ])
说明:
data[ ,2] < 0.05
将返回一个TRUE / FALSE向量,其长度为列。
所以你会得到:
data[c(TRUE, FALSE....), ]
从那时起,只选择数据为TRUE。
因此,您只会绘制p值低于0.05的x和y。
答案 3 :(得分:0)
以下是仅绘制具有显着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)
}
希望这会有所帮助。