Displaying 20 results from an estimated 10000 matches similar to: "Elegant way to subtract matrix from array"
2010 Aug 11
2
Sweeping a zoo series
Given a long zoo matrix, the goal is to "sweep" out a statistic from the
entire length of the
sequences.
longzoomatrix<-zoo(matrix(rnorm(720),ncol=6),as.yearmon(outer(1900,seq(0,length=120)/12,"+")))
cnames<-c(12345,23456,34567,45678,56789,67890)
colnames(longzoomatrix)<-cnames
longzoomatrix[1:24,]
12345 23456 34567 45678
2011 Sep 12
5
Hourly data with zoo
I have date data as a numeric and hourly data in 0 to 2300 hours in a dataframe.
d <- rep(20110101,24)
h <- seq(from = 0, to = 2300, by = 100)
df <- data.frame(LST_DATE = d, LST_TIME = h, data = rnorm(24, 0, 1))
S <- chron(dates. = as.character(df$LST_DATE), times. =
paste(as.character(df$LST_TIME/100), ":0:0", sep = ""),
format =
2012 Mar 29
2
subtract a list of vectors from a list of data.frames in an elegant way
Dear R experts,
I've realized that it might not be possible to define a negative SELCET statement in a SQL call so now I'm looking for the smoothest way to generate a list of what I would like from my large database by first pulling all the names with a query like this "SELECT top 1 * FROM your_table" (thank you Bart Joosen for the idea) and then subtract the variables I am not
2010 Apr 25
3
Noobie question on aggregate tapply and by
I have a 43MB dataframe ( 5 variables) and I'm trying to summarize subsets
of the data.
I've RTFM ( not very clear) and looked at a variety of samples but cant seem
to figure out
how to make these functions work.
A sample of what I want to do would be this:
ids<-seq(1,50)
years<-c(rep(5,10),rep(6,10),rep(7,10),rep(8,20))
2010 Dec 21
1
lm() on a matrix of zoo series
I have a matrix of zoo series. each series is in a column.
x <- as.yearmon(2000 + seq(0, 23)/12)
# 24 months of data, lets make 20 sets of random data
testData <- matrix(rnorm(480),ncol=20)
# make a zoo object and columns will hold the 20 series
TestZoo <- zoo(testData,order.by=x)
# now run lm for just one series.
m <- lm(TestZoo[,1]~time(TestZoo))$coeff[2]
m
time(TestZoo)
2011 May 27
2
help with barplot
Hi,
I'm really struggling with barplot
I have a data.frame with 3 columns. The first column represents an
"incident" type
The second column represents a "month"
The third column represents a "time"
Code for a sample data.frame
incidents <- rep(c('a','b','d','e'), each =25)
months <- rep(c(1,2), each =10)
times
2010 Jul 15
3
Summing over intervals
Given a matrix of MxN
want to take the means of rows in the following fashion
m<-matrix(seq(1,80),ncol=20, nrow=4)
result<-matrix(NA,nrow=4,ncol=20/5)
result[,1]<-apply(m[,1:5],1,mean)
result[,2]<-apply(m[,6:10],1,mean)
result[,3]<-apply(m[,11:15],1,mean)
result[,4]<-apply(m[,16:20],1,mean)
result
[,1] [,2] [,3] [,4]
[1,] 9 29 49 69
[2,] 10 30 50 70
2010 Jun 05
5
Matrix to Vector
Given a matrix of m*n, I want to reorder it as a vector, using a row major
transpose.
so:
> m<-matrix(seq(1,48),nrow=6,byrow=T)
> m
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 1 2 3 4 5 6 7 8
[2,] 9 10 11 12 13 14 15 16
[3,] 17 18 19 20 21 22 23 24
[4,] 25 26 27 28 29 30 31 32
[5,] 33 34 35 36 37
2005 Jun 20
6
sweep() and recycling
Hi
I had a hard-to-find bug in some of my code the other day, which I
eventually
traced to my misusing of sweep().
I would expect sweep() to give
me a warning if the elements don't recycle nicely, but
X <- matrix(1:36,6,6)
sweep(X,1,1:5,"+")
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 2 9 16 23 30 32
[2,] 4 11 18 25 27 34
[3,] 6 13 20 22
2012 Oct 30
3
subtract a time period from a date
Hello everybody,
how can I reduce e.g. 30 days from a date?
When I do the following "2011-05-01 CEST" -"2011-04-01 CEST" I get:
"Time difference of 30 days"
an thats fine.
But when I try "2011-05-01 CEST" - 30 I get nonsense.
So how can I subtract some days, month or years from a date?
thanking you in anticipation
Claudia Paladini
2011 Mar 18
2
Understanding tryCatch
I've read the help and the archives on tryCatch but I'm still stuggling
trying to understand how it
works exactly and how I can use it to get the result I need.
I have a data.frame of urls which point to 11 .zip files. Basically I use
RCurl to get the list of
files from a ftp and then reduce that directory dump to the 11 zip files I
want to download.
Its easy enough to do that in a loop
2012 Nov 29
3
How to subtract the counter i in for loop?
Hi list,
I am writing a for loop that looks like this:
samples<-rep(NA,10)
x <- rep(c(111, 225), 5)
for(i in 1:10){
If(x[i]<200){
samples[i] <- x[i]
}else{
i=i-1
}
}
The problem is that the returning vector still contains NA, I think the i
in "else" is not getting subtracted. How should I get it to work?
Thanks,
Mike
[[alternative HTML version
2010 May 03
2
Adding a header after the file is written
The situation arises where I open a file to write a data.frame to it. with
write.table.
multiple lines are written to the file and the file is kept in Append=TRUE
mode.
If one sets the col.names to the names of the variables being written, you
have output
that looks like this...
name1 name2 name3.....
x x x
x x x
x x x
name1 name2 name
2013 Jan 14
1
hwo to subtract a child array from the big array?
hi R users
I have a data set with the name AA
AA<-1:100
Now I want to get a child array from AA every 10 numbers
e.g.
ab =c(10,20,30,40,50,60,70,80,100)
How could I subtract aa from AA?
thank you .
--
TANG Jie
Email: totangjie@gmail.com
Tel: 0086-2154896104
Shanghai Typhoon Institute,China
[[alternative HTML version deleted]]
2010 Aug 09
1
nested 'by'
Assuming a data frame or matrix with two columns representing variable that
you want to aggregate over.
you want to calculate column means, by year, for each Id
example<-data.frame(id=c(rep(12345,5),rep(54321,6),rep(45678,7)),Year=rep(seq(1900,1902,by=1),6),
x=seq(1,18,by=1),y=seq(18,1,by=-1))
example
id Year x y
1 12345 1900 1 18
2 12345 1901 2 17
3 12345 1902 3 16
4 12345
2011 Nov 01
1
weird error
I was just rebuilding a package that has built before and I hit this error
Error in loadNamespace(package, c(which.lib.loc, lib.loc), keep.source =
keep.source) :
cyclic name space dependency detected when loading 'GhcnDaily', already
loading 'GhcnDaily'
The package built just fine last revision, and the only changes I made were
to Rd files
How do I track this puppy down
2010 May 04
1
error in La.svd Lapack routine 'dgesdd'
Error in La.svd(x, nu, nv) : error code 1 from Lapack routine ‘dgesdd’
what resources are there to track down errors like this
[[alternative HTML version deleted]]
2010 Jun 14
1
recursively Merging a list a zoo objects
The zoo package as a merge function which merges a set of zoo objects
result<-merge(zoo1,zoo2,...)
Assume your zoo objects are already collected in a list
# make a phony list to illustrate the situation. ( hat tip to david W for
constructing a list in a loop)
ddat <- as.list(rep("", 20))
ytd<-seq(3,14)
for(i in 1:20) {
+ ddat[[i]] <- zoo(data,ytd )
+ }
ddat
[[1]]
1 2
2010 Aug 15
1
Trouble loading "saved" Rdata
In the particular application I have I save "test.Rdata" to a sub directory
dir<-"Example"
dir.create(dir)
test<-data.frame(a=c(1,2,3),b=c(3,4,5)
full<-file.path(dir,"test.Rdata,fsep=.Platform$file.sep)
save(test,file=full)
load(full)
returns NULL
it works fine when the object is saved to the working directory, but fails
when saved to a sub directory.
The Rdata
2010 Aug 20
1
differecing a zoo series
A quick question
x <- as.yearmon(2000 + seq(0, 23)/12)
x
[1] "Jan 2000" "Feb 2000" "Mar 2000" "Apr 2000" "May 2000" "Jun 2000" "Jul
2000" "Aug 2000" "Sep 2000" "Oct 2000" "Nov 2000" "Dec 2000" "Jan 2001"
[14] "Feb 2001" "Mar 2001" "Apr