Displaying 20 results from an estimated 700 matches similar to: "no method for coercing this S4 class to a vector"
2012 Aug 24
2
SparseM buglet
read.matrix.csr does not close the connection:
> library('SparseM')
Package SparseM (0.96) loaded.
> read.matrix.csr(foo)
...
Warning message:
closing unused connection 3 (foo)
>
--
Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000
http://www.childpsy.net/ http://truepeace.org http://camera.org
http://pmw.org.il http://think-israel.org
2012 Oct 16
2
cannot coerce class '"rle"' into a data.frame
why?
> rle
Run Length Encoding
lengths: int [1:1650061] 2 2 8 2 4 5 6 3 26 46 ...
values : chr [1:1650061] "4bbf9e94cbceb70c BG bg" "4fbbf2c67e0fb867 SK sk" ...
> as.data.frame(rle)
Error in as.data.frame.default(vertices.rle) :
cannot coerce class '"rle"' into a data.frame
it seems that
rle.df <-
2013 Jan 04
4
non-consing count
Hi,
to count vector elements with some property, the standard idiom seems to
be length(which):
--8<---------------cut here---------------start------------->8---
x <- c(1,1,0,0,0)
count.0 <- length(which(x == 0))
--8<---------------cut here---------------end--------------->8---
however, this approach allocates and discards 2 vectors: a logical
vector of length=length(x) and an
2013 Jan 18
5
select rows with identical columns from a data frame
I have a data frame with several columns.
I want to select the rows with no NAs (as with complete.cases)
and all columns identical.
E.g., for
--8<---------------cut here---------------start------------->8---
> f <- data.frame(a=c(1,NA,NA,4),b=c(1,NA,3,40),c=c(1,NA,5,40))
> f
a b c
1 1 1 1
2 NA NA NA
3 NA 3 5
4 4 40 40
--8<---------------cut
2012 Dec 04
3
list to matrix?
How do I convert a list to a matrix?
--8<---------------cut here---------------start------------->8---
list(c(50000, 101), c(1e+05, 46), c(150000, 31), c(2e+05, 17),
c(250000, 19), c(3e+05, 11), c(350000, 12), c(4e+05, 25),
c(450000, 19), c(5e+05, 16))
as.matrix(a)
[,1]
[1,] Numeric,2
[2,] Numeric,2
[3,] Numeric,2
[4,] Numeric,2
[5,] Numeric,2
[6,] Numeric,2
[7,]
2012 Sep 19
2
drop zero slots from table?
I find myself doing
--8<---------------cut here---------------start------------->8---
tab <- table(...)
tab <- tab[tab > 0]
tab <- sort(tab,decreasing=TRUE)
--8<---------------cut here---------------end--------------->8---
all the time.
I am wondering if the "drop 0" (and maybe even sort?) can be effected by
some magic argument to table() which I fail to discover
2012 Nov 07
3
c weirdness
is there a way to avoid c() appending ".0" and ".1" to seed?
--8<---------------cut here---------------start------------->8---
> c("nons"=1, "seed"=3)
nons seed ## good!
1 3
> c("nons"=1, "seed"=tab[1])
nons seed.0 ## don't want ".0"!
1 2344600
>
2012 Oct 18
3
how to concatenate factor vectors?
How do I concatenate two vectors of factors?
--8<---------------cut here---------------start------------->8---
> a <- factor(5:1,levels=1:9)
> b <- factor(9:1,levels=1:9)
> str(c(a,b))
int [1:14] 5 4 3 2 1 9 8 7 6 5 ...
> str(unlist(list(a,b),use.names=FALSE))
Factor w/ 9 levels "1","2","3","4",..: 5 4 3 2 1 9 8 7 6 5 ...
2012 Nov 19
2
generated list element names
How can I create lists with element names created on the fly?
--8<---------------cut here---------------start------------->8---
> list (foo = 10)
$foo
[1] 10
> list ("foo" = 10)
$foo
[1] 10
> list (paste("f","oo",sep="") = 10)
Error: unexpected '=' in "list (paste("f","oo",sep="") ="
2012 Aug 30
3
apply --> data.frame
Is there a way for an apply-type function to return a data frame?
the closest thing I think of is
foo <- as.data.frame(sapply(...))
names(foo) <- c(....)
is there a more "elegant" way?
Thanks!
--
Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000
http://www.childpsy.net/ http://palestinefacts.org http://dhimmi.com
http://honestreporting.com
2012 Aug 28
5
variable scope
At the end of a for loop its variables are still present:
for (i in 1:10) {
x <- vector(length=100000000)
}
ls()
will print "i" and "x".
this means that at the end of the for loop body I have to write
rm(x)
gc()
is there a more elegant way to handle this?
Thanks.
--
Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000
2012 Nov 09
4
as.data.frame(do.call(rbind,lapply)) produces something weird
The following code:
--8<---------------cut here---------------start------------->8---
> myfun <- function (x) list(x=x,y=x*x)
> z <- as.data.frame(do.call(rbind,lapply(1:3,function(x) c(a=paste("a",x,sep=""),as.list(unlist(list(b=myfun(x),c=myfun(x*x*x))))))))
> z
a b.x b.y c.x c.y
1 a1 1 1 1 1
2 a2 2 4 8 64
3 a3 3 9 27 729
2012 Sep 14
3
aggregate() runs out of memory
I have a large data.frame Z (2,424,185,944 bytes, 10,256,441 rows, 17 columns).
I want to get the result of
table(aggregate(Z$V1, FUN = length, by = list(id=Z$V2))$x)
alas, aggregate has been running for ~30 minute, RSS is 14G, VIRT is
24.3G, and no end in sight.
both V1 and V2 are characters (not factors).
Is there anything I could do to speed this up?
Thanks.
--
Sam Steingold
2012 Dec 27
4
vectorization & modifying globals in functions
I have the following code:
--8<---------------cut here---------------start------------->8---
d <- rep(10,10)
for (i in 1:100) {
a <- sample.int(length(d), size = 2)
if (d[a[1]] >= 1) {
d[a[1]] <- d[a[1]] - 1
d[a[2]] <- d[a[2]] + 1
}
}
--8<---------------cut here---------------end--------------->8---
it does what I want, i.e., modified vector d 100 times.
2012 Sep 19
4
where are these NAs coming from?
I see this:
--8<---------------cut here---------------start------------->8---
> length(which(is.na(z$language)))
[1] 0
> locals <- z[z$country == mycountry,]
> length(which(is.na(locals$language)))
[1] 229
--8<---------------cut here---------------end--------------->8---
where are those locals without the language coming from?!
--
Sam Steingold (http://sds.podval.org/) on
2012 Aug 27
1
write.matrix.csr data conversion
> write.matrix.csr(mx, y = y, file = file)
> table(y)
0 1
5194394 23487
$ cut -d' ' -f1 f | sort | uniq -c
23487 2
5194394 1
i.e., 0 is written as 1 and 1 is written as 2.
why?
is there a way to disable this?
--
Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000
http://www.childpsy.net/ http://palestinefacts.org
2012 Aug 27
1
matrix.csr %*% matrix --> matrix
When a sparse matrix is multiplied by a regular one, the result is
usually not sparse. However, when matrix.csr is multiplied by a regular
matrix in R, a matrix.csr is produced.
Is there a way to avoid this?
Thanks!
--
Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000
http://www.childpsy.net/ http://palestinefacts.org http://truepeace.org
2012 Jul 13
1
LiblineaR: read/write model files?
How do I read/write liblinear models to files?
E.g., if I train a model using the command line interface, I might want
to load it into R to look the histogram of the weights.
Or I might want to train a model in R and then apply it using a command
line interface.
--
Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000
http://www.childpsy.net/
2012 Oct 07
2
a merge() problem
I know it does not look very good - using the same column names to mean
different things in different data frames, but here you go:
--8<---------------cut here---------------start------------->8---
> x <- data.frame(a=c(1,2,3),b=c(4,5,6))
> y <- data.frame(b=c(1,2),a=c("a","b"))
>
2012 Aug 15
3
per-vertex statistics of edge weights
I have a graph with edge and vertex weights, stored in two data frames:
--8<---------------cut here---------------start------------->8---
vertices <- data.frame(vertex=c("a","b","c","d"),weight=c(1,2,1,3))
edges <-