Displaying 20 results from an estimated 7000 matches similar to: "Low level subsetting and S3 subsetting"
2018 Sep 10
0
True length - length(unclass(x)) - without having to call unclass()?
On 09/05/2018 11:18 AM, I?aki Ucar wrote:
> The bottomline here is that one can always call a base method,
> inexpensively and without modifying the object, in, let's say,
> *formal* OOP languages. In R, this is not possible in general. It
> would be possible if there was always a foo.default, but primitives
> use internal dispatch.
>
> I was wondering whether it would be
2017 Mar 05
0
length(unclass(x)) without unclass(x)?
I'm looking for a way to get the length of an object 'x' as given by
base data type without dispatching on class. Something analogous to
how .subset()/.subset2(), e.g. a .length() function. I know that I
can do length(unclass(x)), but that will trigger the creation of a new
object unclass(x) which I want to avoid because 'x' might be very
large.
Here's a dummy example
2018 Sep 05
0
True length - length(unclass(x)) - without having to call unclass()?
On 08/24/2018 07:55 PM, Henrik Bengtsson wrote:
> Is there a low-level function that returns the length of an object 'x'
> - the length that for instance .subset(x) and .subset2(x) see? An
> obvious candidate would be to use:
>
> .length <- function(x) length(unclass(x))
>
> However, I'm concerned that calling unclass(x) may trigger an
> expensive copy
2018 Sep 01
0
True length - length(unclass(x)) - without having to call unclass()?
The solution below introduces a dependency on data.table, but otherwise
it does what you need:
---
# special method for Foo objects
length.Foo <- function(x) {
length(unlist(x, recursive = TRUE, use.names = FALSE))
}
# an instance of a Foo object
x <- structure(list(a = 1, b = list(b1 = 1, b2 = 2)), class = "Foo")
# its length
stopifnot(length(x) == 3L)
# get its length as
2018 Sep 03
0
True length - length(unclass(x)) - without having to call unclass()?
Hi Tomas,
On 09/03/2018 11:49 AM, Tomas Kalibera wrote:
> Please don't do this to get the underlying vector length (or to achieve
> anything else). Setting/deleting attributes of an R object without
> checking the reference count violates R semantics, which in turn can
> have unpredictable results on R programs (essentially undebuggable
> segfaults now or more likely later
2010 Nov 18
3
problems subsetting
Dear all,
I have searched the forums for an answer - and there is plenty of
questions along the same line - but none of the paproaches shown worked
to my problem:
I have a data frame that I get from a csv:
summarystats<-as.data.frame(read.csv(file=f_summary));
where I have the columns Dataset, Class, Type, Category,..
Problem1: I want to find a subset of this frame, based on values in
2018 Aug 24
5
True length - length(unclass(x)) - without having to call unclass()?
Is there a low-level function that returns the length of an object 'x'
- the length that for instance .subset(x) and .subset2(x) see? An
obvious candidate would be to use:
.length <- function(x) length(unclass(x))
However, I'm concerned that calling unclass(x) may trigger an
expensive copy internally in some cases. Is that concern unfounded?
Thxs,
Henrik
2018 Sep 03
2
True length - length(unclass(x)) - without having to call unclass()?
Please don't do this to get the underlying vector length (or to achieve
anything else). Setting/deleting attributes of an R object without
checking the reference count violates R semantics, which in turn can
have unpredictable results on R programs (essentially undebuggable
segfaults now or more likely later when new optimizations or features
are added to the language). Setting attributes
2018 Sep 05
4
True length - length(unclass(x)) - without having to call unclass()?
The bottomline here is that one can always call a base method,
inexpensively and without modifying the object, in, let's say,
*formal* OOP languages. In R, this is not possible in general. It
would be possible if there was always a foo.default, but primitives
use internal dispatch.
I was wondering whether it would be possible to provide a super(x, n)
function which simply causes the
2014 Nov 17
1
common base functions stripping S3 class
Hi all --- this is less a specific question and more general regarding
S3 classes.
I've noticed that quite a few very common default implementations of
generic functions (e.g. `unique`, `[`, `as.data.frame`) strip away
class information.
In some cases, it appears conditionals have been created to re-assign
the class, but only for a few special types.
For example, in `unique.default`, if the
2009 Mar 15
0
Assigning to factor[[i]]
I am a bit confused about the semantics of classes, [, and [[.
For at least some important built-in classes (factors and dates), both
the getter and the setter methods of [ operate on the class, but
though the getter method of [[ operates on the class, the setter
method operates on the underlying vector. Is this behavior
documented? (I haven't found any documentation of it.) Is it
2007 Jul 12
1
[[.data frame and row names
Hi,
I'm wondering why indexing a data frame by row name doesn't work
with [[. It works with [:
> sw <- swiss[1:5,1:2]
> sw["Moutier", "Agriculture"]
[1] 36.5
but not with [[:
> sw[["Moutier", "Agriculture"]]
Error in .subset2(.subset2(x, ..2), ..1) : subscript out of bounds
The problem is really with the row name (and not
2009 Dec 12
1
code for [[.data.frame
Hello.
I'm currently trying to wrap up data frames into OCaml via OCaml-R, and
I'm having trouble with data frame subsetting:
> # x#column 1;;
> Erreur dans (function(x, i, exact) if (is.matrix(i)) as.matrix(x)[[i]] else .subset2(x, :
> l'?l?ment 1 est vide ;
> la partie de la liste d'arguments de 'is.matrix' en cours d'?valuation ?tait :
> (i)
2020 Jun 23
0
subset data.frame at C level
It looks to me like internally .subset2 uses `get1index()`, but this
function is declared in Defn.h, which AFAIK is not part of the exported R
API.
Looking at the code for `get1index()` it looks like it just loops over the
(translated) names, so I guess I just do that [0].
[0]:
https://github.com/r-devel/r-svn/blob/1ff1d4197495a6ee1e1d88348a03ff841fd27608/src/main/subscript.c#L226-L235
On Wed,
2020 Jun 17
2
subset data.frame at C level
Hi,
Hope you are well.
I was wondering if there is a function at C level that is equivalent to
mtcars$carb or .subset2(mtcars, "carb").
If I have the index of the column then the answer would be VECTOR_ELT(df,
asInteger(idx)) but I was wondering if there is a way to do it directly
from the name of the column without having to loop over columns names to
find the index?
Thank you
Best
2010 Dec 07
5
fast subsetting of lists in lists
Hello,
my data is contained in nested lists (which seems not necessarily to be
the best approach). What I need is a fast way to get subsets from the data.
An example:
test <- list(list(a = 1, b = 2, c = 3), list(a = 4, b = 5, c = 6),
list(a = 7, b = 8, c = 9))
Now I would like to have all values in the named variables "a", that is
the vector c(1, 4, 7). The best I could come up
2012 May 03
1
deparse(substitute(x)) on an object with S3 class
Dear list,
can someone explain to me why deparse(substitute(x)) does not seem to work
when x is of a user-defined S3 class?
In my actual problem, my print method is part of a package, and the method
is registered in the NAMESPACE, if that should make a difference.
> print.testclass <- function(x,...){
xname <- deparse(substitute(x))
cat("Your object name
2011 Jun 29
0
Error in testInstalledBasic
Hi,
I am running R 2.13.0 on a Windows 7 machine.
I ran the script:
testInstalledBasic('devel')
and received the following warning message:
running tests of consistency of as/is.*
creating ?isas-tests.R?
running code in ?isas-tests.R?
comparing ?isas-tests.Rout? to ?isas-tests.Rout.save? ...running tests of random deviate generation -- fails occasionally
running code in
2004 Feb 17
1
deprecated 'codes' function in 'factor' docs (PR#6590)
Full_Name: Andrew Stryker
Version: 1.8.1
OS: Linux/Win XP
Submission from: (NULL) (65.124.252.58)
>From ?factor:
See Also:
'[.factor' for subsetting of factors.
'gl' for construction of "balanced" factors and 'C' for factors
with specified contrasts. 'levels' and 'nlevels' for accessing the
levels, and
2011 Aug 07
1
all.equal doesn't work for POSIXlt objects
Hi all,
following sample code illustrates the problem :
Date1 <- Date2 <-
as.POSIXlt(seq.Date(as.Date("2010-04-01"),as.Date("2011-04-01"),by='day'))
identical(Date1,Date2)
all.equal(Date1,Date2)
identical() gives the correct answer. As there is no all.equal method
for POSIXlt objects, all.equal.list is used instead. Subsetting using
[[]] doesn't work