Displaying 10 results from an estimated 10 matches similar to: "creating lagged variables"
2010 Nov 08
4
Random Sample
Hello R users,
Here is my question about generating random sample. How to set the random seed to recreate the same random numbers? For example, 10 random numbers is generated from N(0,1), then "runif(10)" is used.What if I want to get the same 10 random numbers when I run runif(10) again? Is it possible?I think .Random.seed should be used here.
Thanks.
Xiaoxi
[[alternative
2010 Sep 01
2
getting column names of row-by-row sorted matrix
Hi folks,
I want to sort a matrix row-by-row and create a new matrix that contains the corresponding colnames of the original matrix.
E.g.
> set.seed(123)
> a <- matrix(rnorm(20), ncol=4); colnames(a) <- c("A","B","C","D")
> a
A B C D
[1,] -0.56047565 1.7150650 1.2240818 1.7869131
[2,]
2008 May 07
1
Automatically generating new column names (and columns)
Once again I need to tap into the collective knowledge here.
Let's say I have the following columns and data below
Y X1 X2 X3 X4
I would like to generate additional new columns and column names
(ie the data would be squared - and I'd like the column names to
reflect this) like:
Y X1 X2 X3 X4 X1^2 X2^2 X3^2 X4^2
I believe I can compute the values correctly with the code below, but
I
2008 Mar 11
2
Replacing text
Sorry, another newbie question :-(
I loaded a data set with 10 rows and 30 columns. The first column is
characters for names of car manufacturers:
Jeep
Nissan
Toyota1
Toyota2
Etc.
How can I replace "Toyota2" with "Scion"?
Thanks again
[[alternative HTML version deleted]]
2010 Jul 27
6
Eval() or parse() do not work inside function
I am writing a function where the arguments are names of objects or variable
names in a data frame. To convert the strings to the objects I am using
eval(parse(text=name)):
f.graph.two.vbs<-function(dataname,v1){
val<-paste(dataname,v1,sep="$")
val<-eval(parse(text=val))
val
}
However running this returns an error:
2024 Oct 04
2
apply
Hello
I have a vector:
set.seed(123) > n<-3 > x<-rnorm(n); x [1] -0.56047565 -0.23017749
1.55870831 I like to create a matrix with elements containing variances
and covariances of x. That is var(x[1]) cov(x[1],x[2]) cov(x[1],x[3])
cov(x[2],x[1]) var(x[2]) cov(x[2],x[3]) cov(x[3],x[1]) cov(x[3],x[2])
var(x[3]) And I like to do it with "apply". Thanks.
On 10/4/2024 6:35
2024 Oct 04
1
apply
Hello,
This doesn't make sense, if you have only one vector you can estimate
its variance with
var(x)
but there is no covariance, the joint variance of two rv's. "co" or
joint with what if you have only x?
Note that the variance of x[1] or any other vector element is zero, it's
only one value therefore it does not vary. A similar reasonong can be
applied to cov(x[1],
2024 Oct 04
3
apply
OK. Thanks to all. Suppose I have two vectors, x and y. Is there a way
to do the covariance matrix with ?apply?. The matrix I need really
contains the deviation products divided by the degrees of freedom (n-1).
That is, the elements
(1,1), (1,2),...,(1,n)
(2,1), (2,2),...., (2,n)
....
(n,1),(n,2),...,(n,n).
> Hello,
>
> This doesn't make sense, if you have only one vector you
2024 Oct 04
1
apply
It's still hard to figure out what you want. If you have two vectors
you can compute their (2x2) covariance matrix using cov(cbind(x,y)).
If you want to compute all pairwise squared differences between elements
of x and y you could use outer(x, y, "-")^2.
Can you explain a little bit more about (1) the context for your
question and (2) why you want/need to use apply() ?
On
2024 Oct 04
1
apply
Hello,
If you have a numeric matrix or data.frame, try something like
cov(mtcars)
Hope this helps,
Rui Barradas
?s 10:15 de 04/10/2024, Steven Yen escreveu:
> On 10/4/2024 5:13 PM, Steven Yen wrote:
>
>> Pardon me!!!
>>
>> What makes you think this is a homework question? You are not
>> obligated to respond if the question is not intelligent enough for you.