I'm trying to return an object with a value 'alt_props' for all traits with the use of a function. (alt_props x b) should at least be 30, so when the calculated (alt_props x b) is lower than 30, that alt_props value should be changed to the alt_props value of 30/b. (1:7) are the columns that represent the different traits.
calc_props <- function(x, pvals, betas){
s <- colSums(pvals < 5e-8, na.rm = TRUE) #significant SNPs
t <- colSums(!is.na(pvals)) #all SNPs
b <- colSums(!is.na(betas)) #Number of betas
alt_props <- s/t #alt_prop calculation
a[x] <- 30/b #value to assign if alt_props*b < 30
alt_props[(alt_props*b) < 30] <- a[x] #check to ensure that "alt_prop*b" is atleast 30
alt_props
}
alt_props <- calc_props(1:7, pvals, betas) #the columns are the traits (7)
However, when running this code I get the error Error in a[x] <- 30/b : object 'a' not found
. How can i assign the correct a?
My data looks like this:
dput(pvals[1:10])
c(0.14, 0.87, 3.7e-23, 1.2e-07, 0.84, 0.72, 0.34, 0.13, 0.019,
8e-05)
> dput(betas[1:10])
c(-0.0021, 2e-04, -0.0141, -0.0082, -7e-04, 8e-04, 0.0021, -0.0034,
-0.0039, 0.0179)
Second attempt at this answer. Juding by the use of colSums
, s
, t
and b
should all be vectors of the same length, and you calculate the proportions and store that in the vector alt_props
. Now, you want to input the value 30/b
if the condition (alt_props * b) < 30
. We can try to do this using a value and condition vector. See the new function. I couldn't see the need for the x
in the original function since it appeared to only be used to subset a
somehow.
calc_props <- function(x, pvals, betas){
s <- colSums(pvals < 5e-8, na.rm = TRUE)
t <- colSums(!is.na(pvals)) #all SNPs
b <- colSums(!is.na(betas)) #Number of betas
alt_props <- s/t #alt_prop calculation
# Create the subsetting values and condition
condition_val <- 30/b
condition <- (alt_props*b) < 30
# Subset both the alt_props and condition values with the same vector
alt_props[condition] <- condition_val[condition]
alt_props
}