Displaying 20 results from an estimated 50000 matches similar to: "Subset doesn't drop unused factor levels"
2009 Nov 10
3
drop unused levels in subset.data.frame
Dear list,
subset has a 'drop' argument that I had often mistaken for the one in
[.factor which removes unused levels.
Clearly it doesn't work that way, as shown below,
d <- data.frame(x = factor(letters[1:15]), y = factor(LETTERS[1:3]))
s <- subset(d, y=="A", drop=TRUE)
str(s)
'data.frame': 5 obs. of 2 variables:
$ x: Factor w/ 15 levels
2018 Jun 08
6
Subsetting the "ROW"s of an object
Hi all,
Is there a better to way to subset the ROWs (in the sense of NROW) of
an vector, matrix, data frame or array than this?
subset_ROW <- function(x, i) {
nd <- length(dim(x))
if (nd <= 1L) {
x[i]
} else {
dims <- rep(list(quote(expr = )), nd - 1L)
do.call(`[`, c(list(quote(x), quote(i)), dims, list(drop = FALSE)))
}
}
subset_ROW(1:10, 4:6)
#> [1] 4 5 6
2006 Sep 01
3
histograms
I am interested in plotting histograms for the following data
Isoform
Tumor_65_198
Tumor_50_192
Tumor_80_167
Tumor_80_204
Tumor_95_197
Tumor_70_189
Tumor_90_202
Tumor_40_177
Tumor_60_21
Tumor_70_174
Tumor_70_147
Tumor_50_5
ABCC4-2007
1
1
1
6
1
9
10
1
2
0
10
1
ABCC4-2008
5
8
7
5
3
10
5
5
7
3
10
3
ABCC4-2009
0
0
0
0
0
0
0
0
0
0
0
0
ABCC4-2010
2018 Jun 08
2
Subsetting the "ROW"s of an object
I suspect this will have suboptimal performance since the TRUEs will
get recycled. (Maybe there is, or could be, ALTREP, support for
recycling)
Hadley
On Fri, Jun 8, 2018 at 10:16 AM, Berry, Charles <ccberry at ucsd.edu> wrote:
>
>
>> On Jun 8, 2018, at 8:45 AM, Hadley Wickham <h.wickham at gmail.com> wrote:
>>
>> Hi all,
>>
>> Is there a better to
2003 Nov 10
5
Subsetting a list of vectors
Hi,
I'm trying to subset a list which contains variable length vectors.
What I want to do is extract (eg.) the 3rd item in each vector (with
length >= 3). At the moment I'm using sapply(list.of.vectors,
function(x) {x[3]}). The problem with this is that sapply returns a
list of the same length of list.of.vectors so I end up with a whole lot
of null entries from those vectors
2004 Jun 27
2
subset drop unused levels
hi there
tried to use subset with drop=TRUE, but all the 'old' levels are preserved,
i.e. when calling e.g. ftable a lot of zeros are displayed
>x<-subset(LREG, (kir=='AA' | kir=='BB') & (type=='t1' | otype=='t2'),
drop=TRUE, select=c(event, kir, type))
> ftable(x)
i explicit have to call factor like
2018 Jun 08
3
Subsetting the "ROW"s of an object
> On Jun 8, 2018, at 10:37 AM, Herv? Pag?s <hpages at fredhutch.org> wrote:
>
> Also the TRUEs cause problems if some dimensions are 0:
>
> > matrix(raw(0), nrow=5, ncol=0)[1:3 , TRUE]
> Error in matrix(raw(0), nrow = 5, ncol = 0)[1:3, TRUE] :
> (subscript) logical subscript too long
OK. But this is easy enough to handle.
>
> H.
>
> On
2015 Oct 07
4
authorship and citation
On Tue, Oct 6, 2015 at 4:58 PM, Adrian Du?a <dusa.adrian at unibuc.ro> wrote:
> Hi Gabriel,
>
> On Tue, Oct 6, 2015 at 10:59 PM, Gabriel Becker <gmbecker at ucdavis.edu>
> wrote:
>
>> [...]
>>
>> At the very least, this is seems to be a flagrant violation of the
>> *spirit* of the CRAN policy, which AFAIK is intended to enforce
>>
2006 Sep 15
2
dotplot, dropping unused levels of 'y'
In dotplot, what's the best way to suppress the unused levels of 'y' on
a per-panel basis? This is useful for the case that 'y' is a factor
taking perhaps thousands of levels, but for a given panel, only a
handfull of these levels ever present.
Thanks,
Ben
2018 Jun 08
4
Subsetting the "ROW"s of an object
> On Jun 8, 2018, at 11:52 AM, Hadley Wickham <h.wickham at gmail.com> wrote:
>
> On Fri, Jun 8, 2018 at 11:38 AM, Berry, Charles <ccberry at ucsd.edu> wrote:
>>
>>
>>> On Jun 8, 2018, at 10:37 AM, Herv? Pag?s <hpages at fredhutch.org> wrote:
>>>
>>> Also the TRUEs cause problems if some dimensions are 0:
>>>
2009 Oct 05
1
Characters vs. factors
It seems like a recent trend in R has been to make character vectors
and factors almost equivalent (apart from the way that factors always
remember their original range). There are a few exceptions:
* summary.character != summary.factor
* table(x, exclude = NULL) != table(factor(x), exclude=NULL) when x
includes missing values
* strsplit on a factor
> strsplit(factor(c("a",
2010 Feb 04
4
Why is there no c.factor?
Hi all,
Is there are reason that there is no c.factor method? Analogous to
c.Date, I'd expect something like the following to be useful:
c.factor <- function(...) {
factors <- list(...)
levels <- unique(unlist(lapply(factors, levels)))
char <- unlist(lapply(factors, as.character))
factor(char, levels = levels)
}
c(factor("a"), factor("b"),
2004 Nov 10
1
List seems to drop empty levels of factors when containing them
Greetings R community,
I am curious about the following behaviour: if I define a factor, and
then store a subset of it in a list, the stored version seems to drop
levels that were not included in the subset. E.g. ..
> mask <- c(T, F)
> grp.1 <- factor(c(1,2))
> list(grp.1)
[[1]]
[1] 1 2
Levels: 1 2
> list(grp.1[mask])
[[1]]
[1] 1
Levels: 1
It is as though the list were
2018 Jun 08
1
Subsetting the "ROW"s of an object
On 06/08/2018 10:15 AM, Michael Lawrence wrote:
> There probably should be an abstraction for this. In S4Vectors, we
> have extractROWS().
FWIW the code in S4Vectors that does what your subset_ROW() does is:
https://github.com/Bioconductor/S4Vectors/blob/04cc9516af986b30445e99fd1337f13321b7b4f6/R/subsetting-utils.R#L466-L476
(This is the default "extractROWS" method.)
Except
2018 Jun 08
3
Subsetting the "ROW"s of an object
> On Jun 8, 2018, at 1:49 PM, Hadley Wickham <h.wickham at gmail.com> wrote:
>
> Hmmm, yes, there must be some special case in the C code to avoid
> recycling a length-1 logical vector:
Here is a version that (I think) handles Herve's issue of arrays having one or more 0 dimensions.
subset_ROW <-
function(x,i)
{
dims <- dim(x)
index_list <-
2016 Aug 15
2
ifelse() woes ... can we agree on a ifelse2() ?
On Fri, Aug 12, 2016 at 11:31 AM, Hadley Wickham <h.wickham at gmail.com> wrote:
>> >> One possibility would also be to consider a "numbers-only" or
>> >> rather "same type"-only {e.g., would also work for characters}
>> >> version.
>>
>> > I don't know what you mean by these.
>>
>> In the
2007 Oct 15
2
Digest package - make digest generic?
On 10/15/07, Henrik Bengtsson <hb at maths.lth.se> wrote:
> [As agreed, CC:ing r-devel since others might be interested in this as well.]
>
> Hi.
>
> On 10/15/07, Dirk Eddelbuettel <edd at debian.org> wrote:
> >
> > Hi Hadley,
> >
> > On 15 October 2007 at 09:51, hadley wickham wrote:
> > | Would you consider making digest a generic
2015 Jun 30
2
Defining a `show` function breaks the print-ing of S4 object -- bug or expected?
On 30/06/2015 5:27 PM, Lorenz, David wrote:
> There is something I'm really missing here. The function show is a
> standardGeneric function, so the correct way to write it as method like
> this:
That describes methods::show. The problem is that the default print
mechanism isn't calling methods::show() (or base::print() as Luke says),
it's calling show() or print() in the
2016 Nov 15
2
ifelse() woes ... can we agree on a ifelse2() ?
All,
Martin: Thanks for this and all the other things you are doing to both
drive R forward and engage more with the community about things like this.
Apologies for missing this discussion the first time it came around and if
anything here has already been brought up, but I wonder what exactly you
mean when you want recycling behavior.
Specifically, based on an unrelated discussion with Henrik
2012 Apr 21
2
using "factor" to eliminate unused levels without dropping other variables
Hello,
I have been banging my head against the wall trying to figure out this
seemingly simple problem with no success. I'm hoping that one or some
of you can help.
Here is the code I am trying to use:
#importing data
data.file <-read.csv("/file/location", header=TRUE, sep = ",")
#selecting a subset of data based on variable "Sample"
data.subset1 <-