Displaying 20 results from an estimated 40000 matches similar to: "Array arithmetic"
2009 Jan 06
5
Using apply for two datasets
I can run one-sample t-test on an array, for example a matrix myData1,
with the following
apply(myData1, 2, t.test)
Is there a similar fashion using apply() or something else to run
2-sample t-test with datasets from two groups, myData1 and myData2,
without looping?
TIA,
Gang
2008 Feb 28
3
Collapse an array
Suppose I have a 4-D array X with dimensions (dx, dy, dz, dp). I want
to collapse the first 3 dimensions of X to make a 2-D array Y with
dimensions (dx*dy*dz, dp). Instead of awkward looping, what is a good
way to do this? Is there a similar function like reshape in Matlab?
Thanks,
Gang
2017 Jul 06
3
Efficient swapping
Thanks a lot, Ista! I really appreciate it.
How about a slightly different case as the following:
set.seed(1)
(tmp <- data.frame(x = 1:10, R1 = sample(LETTERS[1:5], 10, replace =
TRUE), R2 = sample(LETTERS[2:6], 10, replace = TRUE)))
x R1 R2
1 C B
2 B B
3 C E
4 E C
5 E B
6 D E
7 E E
8 D F
9 C D
10 A E
Notice that the factor levels between
2008 Apr 07
3
Extract values from a named array
Sorry for this dumb question. Suppose I have a named array ww defined as
ww <- 1:5
names(ww) <- c("a", "b", "c", "d", "e")
How can I extract the whole array of numbers without the names?
ww[1:5] does not work while ww[[1]] can only extract one number at a
time.
Thanks,
Gang
2017 Jul 06
2
Efficient swapping
Suppose that we have the following dataframe:
set.seed(1)
(tmp <- data.frame(x = 1:10, R1 = sample(LETTERS[1:5], 10, replace =
TRUE), R2 = sample(LETTERS[1:5], 10, replace = TRUE)))
x R1 R2
1 1 B B
2 2 B A
3 3 C D
4 4 E B
5 5 B D
6 6 E C
7 7 E D
8 8 D E
9 9 D B
10 10 A D
I want to do the following: if the difference between the level index
of factor
2017 Jul 06
0
Efficient swapping
Untested, but I expect that setting the levels to be the same across the
two factors
levels(tmp$R1) <- levels(tmp$R2) <- LETTERS[1:6]
and proceeding as before should be fine.
Best,
Ista
On Jul 6, 2017 6:54 PM, "Gang Chen" <gangchen6 at gmail.com> wrote:
Thanks a lot, Ista! I really appreciate it.
How about a slightly different case as the following:
set.seed(1)
(tmp
2011 Mar 10
3
A question about data frame
A very simple question. With a data frame like this:
> n = c(2, 3, 5)
> s = c("aa", "bb", "cc")
> df = data.frame(n, s)
I want df$s[1] or df[1,2], but how can I get rid of the extra line in
the output about the factor levels:
> df$s[1]
[1] aa
Levels: aa bb cc
Thanks,
Gang
2012 Feb 07
6
Setting up infile for R CMD BATCH
Suppose I create an R program called myTest.R with only one line like
the following:
type <- as.integer(readline("input type (1: type1; 2: type2)? "))
Then I'd like to run myTest.R in batch mode by constructing an input
file called answers.R with the following:
source("myTest.R")
1
When I ran the following at the terminal:
R CMD BATCH answer.R output.Rout
it failed
2010 Mar 27
3
Calculate variance/covariance with complex numbers
Anybody knows what functions can be used to calculate
variance/covariance with complex numbers? var and cov don't seem to
work:
> a
1
V1 0.00810014+0.00169366i
V2 0.00813054+0.00158251i
V3 0.00805489+0.00163295i
V4 0.00809141+0.00159533i
V5 0.00813976+0.00161850i
> var(a)
1
1 1.141556e-09
Warning message:
In var(a) : imaginary parts discarded in
2013 Dec 14
2
Change factor levels
Suppose I have a dataframe 'd' defined as
L3 <- LETTERS[1:3]
d0 <- data.frame(cbind(x = 1, y = 1:10), fac = sample(L3, 10, replace
= TRUE))
(d <- d0[d0$fac %in% c('A', 'B'),])
x y fac
2 1 2 B
3 1 3 A
4 1 4 A
5 1 5 A
6 1 6 B
8 1 8 A
Even though factor 'fac' in 'd' only has 2 levels, but it seems to bear the
birthmark
2010 Feb 05
3
metafor package: effect sizes are not fully independent
In a classical meta analysis model y_i = X_i * beta_i + e_i, data
{y_i} are assumed to be independent effect sizes. However, I'm
encountering the following two scenarios:
(1) Each source has multiple effect sizes, thus {y_i} are not fully
independent with each other.
(2) Each source has multiple effect sizes, each of the effect size
from a source can be categorized as one of a factor levels
2017 Jul 06
0
Efficient swapping
How about
foo <- with(list(r1 = tmp$R1,
r2 = tmp$R2,
swapme = (as.numeric(tmp$R1) - as.numeric(tmp$R2)) %% 2 != 0),
{
tmp[swapme, "R1"] <- r2[swapme]
tmp[swapme, "R2"] <- r1[swapme]
tmp
})
Best,
Ista
On Thu, Jul 6, 2017 at 4:06 PM, Gang Chen <gangchen6 at gmail.com> wrote:
> Suppose that we have the following
2011 May 22
2
Convert dataframe with two factors from wide to long format
I know how to convert a simple dataframe from wide to long format with one
varying factor. However, for a dataset with two factors like the following,
Subj T1_Cond1 T1_Cond2 T2_Cond1 T2_Cond2
1 0.125869 4.108232 1.099392 5.556614
2 1.427940 2.170026 0.120748 1.176353
How to elegantly convert to a long form as
Subj Time Cond Value
1 1 1 0.125869
1
2011 Oct 06
3
Wide to long form conversion
I have some data 'myData' in wide form (attached at the end), and
would like to convert it to long form. I wish to have five variables
in the result:
1) Subj: factor
2) Group: between-subjects factor (2 levels: s / w)
3) Reference: within-subject factor (2 levels: Me / She)
4) F: within-subject factor (2 levels: F1 / F2)
5) J: within-subject factor (2 levels: J1 / J2)
As this is the
2009 Jan 23
1
Interpreting model matrix columns when using contr.sum
With the following example using contr.sum for both factors,
> dd <- data.frame(a = gl(3,4), b = gl(4,1,12)) # balanced 2-way
> model.matrix(~ a * b, dd, contrasts = list(a="contr.sum", b="contr.sum"))
(Intercept) a1 a2 b1 b2 b3 a1:b1 a2:b1 a1:b2 a2:b2 a1:b3 a2:b3
1 1 1 0 1 0 0 1 0 0 0 0 0
2 1 1 0 0 1 0
2008 Aug 07
2
Creating an array of lists
Hi,
I want to store some number of outputs from running a bunch of
analyses such as lm() into an array. I know how to do this with a
one-dimensional array (vector) by creating
myArray <- vector(mode='list', length=10)
and storing each lm() result into a component of myArray.
My question is, how can do this for a multiple dimensional array? It
seems array() does not have such a
2008 Apr 16
2
Post hoc tests with lme
Using the "ergoStool" data cited in Mixed-Effects Models in S and
S-PLUS by Pinheiro and Bates as an example, we have
========
> library(nlme)
> fm <- lme(effort~Type-1, data=ergoStool, random=~1|Subject)
> summary(fm)
Linear mixed-effects model fit by REML
Data: ergoStool
AIC BIC logLik
133.1308 141.9252 -60.5654
Random effects:
Formula: ~1 | Subject
2004 Mar 31
2
array addition doesn't recycle!
Hi,
I have noticed the following:
> a <- array(1:4, c(2, 2))
> A <- array(1:4, c(2,2,2))
> A + a
Error in A + a : non-conformable arrays
It works with a matrix + a vector, why doesn't it work with arrays?
Am I missing something?
How would you do the above operation efficiently (ie I need to add a
matrix to each "plane" of 3-dim array)? At the moment I am using
2012 May 14
3
How to apply a function to a multidimensional array based on its indices
Hello. I have a 4 dimensional array and I want to fill in the slots with
values which are a function of the inputs. Through searching the forums here
I found that the function "outer" is helpful for 2x2 matrices but cannot be
applied to general multidimensional arrays. Is there anything which can
achieve, more efficiently than the following code, the job I want?
K <-
2008 Oct 02
4
Load a program at the front end
I want to run a R program, prog.R, interactively. My question is, is
there a way I can start prog.R on the shell terminal when invoking R,
instead of using source() inside R?
TIA,
Gang