0

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)
Noa
  • 55
  • 5
  • It is exactly what the error says: `a` does not exist. `a` is not defined inside the function nor outside, I would assume. You are trying to index an object `a` that can't be found in the search path. What is `a` supposed to be? Also in the future, please provide some sample data and a reprodusible example including what you expect as the result. It will make it easier for people to help you. – edsandorf May 06 '20 at 09:08
  • @edsandorf I will keep that in mind for the future. `a` should be a temporary value that can be assigned to a `alt_props` value when the calculated `alt_props` value does not meet the criteria (namely `alt_props*b should be > 30`. When this criteria is not met, `alt_props` should be changed to the value causing `alt_props * b)` to be 30 --> this should be `a` – Noa May 06 '20 at 09:34
  • suggestions for reproducible examples: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – desval May 06 '20 at 10:11

1 Answers1

0

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
}
edsandorf
  • 757
  • 7
  • 17
  • `Warning message: In alt_props[(alt_props * b) < 30] <- 30/b : number of items to replace is not a multiple of replacement length`. Unfortunately I also get the same error when using a vector – Noa May 06 '20 at 10:16
  • I've added the pvals and betas data to my question. Is it reproducible now? – Noa May 06 '20 at 10:20
  • It was not. It `pvals`and `betas` are matrices, you supplied vectors. I made a second attempt at a solution – edsandorf May 06 '20 at 10:42