p.adjust error: 'oderVector1' - r

I'm having a problem with the function p.adjust. I have a list containing 741 p-values and I want to use the p.adjust() function to correct for multiple testing (FDR testing). This is what I have so far:
> x <- as.vector(pvalues1)
> p.adjust(x, method="fdr" n=length(x))
But I get the following error
Error in order (p, decreasing = TRUE) :
unimplemented type 'list' in 'orderVector1'
Can anyone help me with this?

The problem you have is the your list containing the p-values is a vector already. What you wanted was a numeric vector. A list is just a general vector:
> l <- list(A = runif(1), B = runif(1))
> l
$A
[1] 0.7053136
$B
[1] 0.7053284
> as.vector(l)
$A
[1] 0.7053136
$B
[1] 0.7053284
> is.vector(l)
[1] TRUE
One option is to unlist() the list, to produce a numeric vector:
> unlist(l)
A B
0.7053136 0.7053284
the benefit of that is that it preserves the names. An alternative is plain old as.numeric(), which looses the names, but is otherwise the same as unlist():
> as.numeric(l)
[1] 0.7053136 0.7053284
For big vectors, you might not want to use the names in unlist(), so an alternative that will speed that version up is:
> unlist(l, use.names = FALSE)
[1] 0.7053136 0.7053284

Related

Subset operator for S3 classes - how to detect [i] versus [i,]

I am implementing a replacement for the subset operator in an S3 class. I followed the advice on
How to define the subset operators for a S4 class?
However I am having a special problem. How do I distinguish in R code if someone wrote x[i] or x[i,]. In both cases, the variable j just comes back missing.
setOldClass("myclass")
'[.myclass' <- function(x, i, j, ..., drop=TRUE) {
print(missing(j))
return(invisible(NULL))
}
And as a result I get:
x <- structure(list(), class="myclass")
> x[i]
[1] TRUE
> x[i,]
[1] TRUE
> x[i,j]
[1] FALSE
I don't see a way on how to distinguish between the two. I assume the internal C code does it by looking at the length of the argument pairlist, but is there a way to do the same in native R?
Thanks!
From alexis_laz's comment:
See, perhaps, how [.data.frame handles arguments and nargs()
Inside the function call nargs() to see how many arguments were supplied, including missing ones.
> myfunc = function(i, j, ...) {
+ nargs()
+ }
>
> myfunc()
[1] 0
> myfunc(, )
[1] 2
> myfunc(, , )
[1] 3
> myfunc(1)
[1] 1
> myfunc(1, )
[1] 2
> myfunc(, 1)
[1] 2
> myfunc(1, 1)
[1] 2
This should be enough to help you figure out which arguments were passed in the same fashion as [.data.frame.

In R, how can I construct an anonymous list containing an element whose name is contained in a variable?

I would like to insert an element into a list in R. The problem is that I would like it to have a name contained within a variable.
> list(c = 2)
$c
[1] 2
Makes sense. I obviously want a list item named 'c', containing 2.
> a <- "b"
> list(a = 1)
$a
[1] 1
Whoops. How do I tell R when i want it to treat a word as a variable, instead of as a name, when I am creating a list?
Some things I tried:
> list(eval(a)=2)
Error: unexpected '=' in "list(eval(a)="
> list(a, 2)
[[1]]
[1] "b"
[[2]]
[1] 2
> list(get(a) = 2)
Error: unexpected '=' in "list(get(a) ="
I know that if I already have a list() laying around, I could do this:
> ll<-list()
> ll[[a]]=456
> ll
$b
[1] 456
...But:
> list()[[a]]=789
Error in list()[[a]] = 789 : invalid (NULL) left side of assignment
How can I construct an anonymous list containing an element whose name is contained in a variable?
One option:
a <- "b"
> setNames(list(2),a)
$b
[1] 2
or the somewhat more "natural":
l <- list(2)
names(l) <- a
and if you look at the code in setNames you'll see that these two methods are so identical that "the way to do this" in R really is basically to create the object, and then set the names, in two steps. setNames is just a convenient way to do that.

R: f(x) != sapply(x,f) -- bug or feature?

> f = function(x) as.Date(as.character(x), format='%Y%m%d')
> f(20110606)
[1] "2011-06-06"
> sapply(20110606, f)
[1] 15131
Why 2 returned values are not the same. I need to apply this function to a long vector of dates, but I'm not getting dates with sapply()!
The functions you use to create f are already vectorized. There's no need to use sapply, unless you work for the Department of Redundancy Department.
> f <- function(x) as.Date(as.character(x), format='%Y%m%d')
> d <- 20110606 + 0:10
> f(d)
[1] "2011-06-06" "2011-06-07" "2011-06-08" "2011-06-09"
[5] "2011-06-10" "2011-06-11" "2011-06-12" "2011-06-13"
[9] "2011-06-14" "2011-06-15" "2011-06-16"
> lapply(20110606, f)
[[1]]
[1] "2011-06-06"
> unlist(lapply(20110606, f))
[1] 15131
sapply unlists lapply and in doing so unclasses the date
> unclass(lapply(20110606, f)[[1]])
[1] 15131
> class(lapply(20110606, f)[[1]])
[1] "Date"
as #Joshua Ulrich noted there is no need to use apply type functions however for interest
d <- 20110606 + 0:10
do.call("c",lapply(d, f))
would be one possible way to "unlist" the dates

as.matrix not preserving the data mode of an empty data.frame

I have found something odd today, I wanted to ask you if there was a logical reason for what I am seeing, or if you think this is a bug that should be reported to the R-devel team:
df <- data.frame(a = 1L:10L)
class(df$a)
# [1] "integer"
m <- as.matrix(df)
class(m[, "a"])
# [1] "integer"
No surprise so far: as.matrix preserves the data mode, here "integer". However, with an empty (no rows) data.frame:
df <- data.frame(a = integer(0))
class(df$a)
# [1] "integer"
m <- as.matrix(df)
class(m[, "a"])
# [1] "logical"
Any idea why the mode changes from "integer" to "logical" here? I am using version 2.13.1
Thank you.
This is because of this one line in as.matrix.data.frame:
if (any(dm == 0L)) return(array(NA, dim = dm, dimnames = dn))
Basically, if any dimensions are zero, you get an array "full" of NA. I say "full" because there aren't really any observations because one of the dimensions is zero.
The reason the class is logical is because that's the class of NA. There are special NA for other classes, but they're not really necessary here. For example:
> class(NA)
[1] "logical"
> class(NA_integer_)
[1] "integer"
> class(NA_real_)
[1] "numeric"
> class(NA_complex_)
[1] "complex"
> class(NA_character_)
[1] "character"

Create "missing objects" (aka: "empty symbols" , "empty objects") / needed for formals manipulation/

How to create an "empty object" in R? [edit: I don't know how to rightly call this "thing" so I am calling it "empty object", others: "empty symbol", "zero length symbol", "missing object" might also be used]
[edit2: finally I tend to settle down on the "missing symbol object" for the name of the "thing". It also appears that J.Chambers is using this terminology in his 2008 book, see comments for #mbq's answer. According to Chambers, the "missing symbol" has a zero-length string as it's contents. Thus, as.symbol("") should create such an object, which it doesn't in current version of R(2.11.1) ]
The simplest way I could find is
x <- alist(a=)$a
[Clarification]
Note that "empty object" is not a NULL object or a vector of length 0.
"Empty object" x in my above example could be used in the function's formals manipulation, which is what I need it for.
Here is an example:
> al <- alist(a = 323, b = , c = 434)
> al
$a
[1] 323
$b
$c
[1] 434
>
> al[["c"]] <- numeric()
> al
$a
[1] 323
$b
$c #not empty
numeric(0)
>
> al[["c"]] <- list()
> al
$a
[1] 323
$b
$c #not empty
list()
>
>
> al[["c"]] <- NULL #object removed
> al
$a
[1] 323
$b
>
> al[["c"]] <- alist(a = )$a
> al
$a
[1] 323
$b
$c #empty
So, I am just looking for a way to create empty objects for use in function's formals manipulations. I am pretty sure there must be a way in base R.
Here is an example:
> foo <- function(a = 3232, b = 234){b+a}
> formals(foo)
$a
[1] 3232
$b
[1] 234
> formals(foo)$c <- alist(a = )$a
> formals(foo)
$a
[1] 3232
$b
[1] 234
$c
> foo <- function(a = 3232, b = 234){b+a}
> formals(foo)
$a
[1] 3232
$b
[1] 234
> formals(foo)$c <- alist(a = )$a
> formals(foo)
$a
[1] 3232
$b
[1] 234
$c
Thanks.
Try substitute():
foo <- function(a = 3232, b = 234) {}
formals(foo)$c <- substitute()
foo
# function (a = 3232, b = 234, c)
What have you done with alist(a=)$a is not an empty object, it is empty symbol/name ('' compiled as symbol name). I'm pretty sure it can't be replicated in any other way (as.symbol('') raises error).
As others have said, you're not creating an empty object. x holds a value, which is an unevaluated expression.
?alist says:
‘alist’ handles its arguments as if they described function arguments.
So the values are not evaluated, and tagged arguments with no value are
allowed whereas ‘list’ simply ignores them. ‘alist’ is most often used in
conjunction with ‘formals’.
This is the easiest way I can determine to add an argument with no value to a function:
> foo <- function(a = 3232, b = 234){b+a}
> formals(foo) <- c(formals(foo),alist(c=))
> formals(foo)
$a
[1] 3232
$b
[1] 234
$c
Depending on the type :
x <- list()
# empty vectors
y <- numeric(0)
z <- logical(0)
...
Now you have to think whether you really want to create an empty object.
edit : You can indeed use the NULL option as well. The main difference is that the NULL object doesn't have a type. See also ?"NULL"
There is no 'empty object'. There's NULL, NA (in various flavours), zero-length lists and so on. Where does your concept of 'empty object' come from, and what are the properties of an 'empty object'?
What you have created with alist is a monster:
> x<-alist(a=)$a
> x
Error: argument "x" is missing, with no default
[its some kind of unevaluated function argument expression thing. Not an 'empty object' in any sense of the word I know]
Also d <- c()
Also d <- ""
(padding padding padding)

Resources