Displaying 20 results from an estimated 40000 matches similar to: "t.test() on a list"
2007 Nov 07
2
Trouble in creating a list
I want to create a list based on the information from a data.frame,
Model. So I tried the following:
MyList <- list(colnames(Model)[2] = levels(Model$(colnames(Model)[2])))
but it failed with an error:
Error: unexpected '=' in "list(colnames(Model)[2] ="
I have the following problems with this command line:
(1) I wanted to use colnames(Model)[2] as a tag for the list:
2013 Feb 12
3
grabbing from elements of a list without a loop
Hello!
# I have a list with several data frames:
mylist<-list(data.frame(a=1:2,b=2:3),
data.frame(a=3:4,b=5:6),data.frame(a=7:8,b=9:10))
(mylist)
# I want to grab only one specific column from each list element
neededcolumns<-c(1,2,0) # number of the column I need from each element of
the list
# Below, I am doing it using a loop:
newlist<-NULL
for(i in 1:length(mylist) ) {
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
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
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
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
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
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
2011 Apr 05
1
Help in splitting a list
Dear R users,
Let's say I have a list with components being 'm' matrices (as exemplified
in the "mylist" object below). Now, I'd like to subset this list based on an
index vector, which will partition each matrix 'm' in 2 sub-matrices. My
questions are:
1. Is there an elegant way to have the results shown in mylist2 for an
arbitrary number of matrices in mylist?
2012 Jun 26
2
flatten lists
I am looking for a function to flatten a list to a list of only 1
level deep. Very similar to unlist, however I don't want to turn it
into a vector because then everything will be casted to character
vectors:
x <- list(name="Jeroen", age=27, married=FALSE,
home=list(country="Netherlands", city="Utrecht"))
unlist(x)
This function sort of does it:
flatlist
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
2005 May 06
2
2 simple questions
Please excuse what I'm sure are very easy questions but I'm relatively new
to the R environment.
How can I view a range of list elements, but not all. e.g., I had a matrix
of patients and then split them out by patient id. I know I can do
patlist[[1]] to see the first one, but how can I view, say, the first ten
patients?
My other question is how to count how many patients have a
2001 Oct 18
2
Parsing for list components
How do I parse an identifier of a list component, e.g.
mylist$mycomponent
or
mylist[[1]] ?
Parse does not do the job, e.g.
parse(text="mylist$mycomponent")
returns an expression with just one term, instead of "mylist", "$",
"mycomponent".
What I need is a way to extract the list name (e.g. "mylist"), given
an identifier of a component.
1999 May 09
1
subscripting in list() (PR#187)
Sorry My previous report is not detailed.
In R, you will get this:
> mylist <- list()
> mylist[[1]]
Error in mylist[[1]] : subscript out of bounds
> mylist[[1]] <- c(1)
Error: (list) object cannot be coerced to vector type 14
> mylist[[1]] <- c(1,2)
> mylist[[1]] <- c(1)
> mylist
[[1]]
[1] 1
I was trying to assigning c(1) to (mylist[[1]] <- c(1)) -- it seems
2008 Mar 06
2
Array arithmetic
I have two arrays A and B with dimensions of (L, M, N, P) and (L, M,
N), and I want to do
for (i in 1:L) {
for (j in 1:M) {
for (k in 1:N) {
if (abs(B[i, j, k]) > 10e-5) C[i, j, k,] <- A[i, j, k,]/B[i, j, k]
else C[i, j, k,] <- 0
}
}
}
How can I get C more efficiently than looping?
Thanks,
Gang
2012 Aug 28
3
Get variable data Reading from the list
Here i have a variable
MyVar <- data.frame(read.csv("D:\\Doc.csv"))
And now i am storing this variable name into a list.
MyList <- list()
MyList [length(MyList )+1]<- "MyVar"
Now what is the requirement is,
i need to call the variable name "MyVar" from the list "MyList " and get
the data.
2010 May 17
3
applying quantile to a list using values of another object as probs
Hi r-users,
I have a matrix B and a list of 3x3 matrices (mylist). I want to
calculate the quantiles in the list using each of the value of B as
probabilities.
The codes I wrote are:
B <- matrix (runif(12, 0, 1), 3, 4)
mylist <- lapply(mylist, function(x) {matrix (rnorm(9), 3, 3)})
for (i in 1:length(B))
{
quant <- lapply (mylist, quantile, probs=B[i])
}
But quant
2011 May 25
3
Accessing elements of a list
I have a list that is made of lists of varying length. I wish to create a
new vector that contains the last element of each list. So far I have used
sapply to determine the length of each list, but I'm stymied at the part
where I index the list to make a new vector containing only the last item
of each list
mylist =
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