Displaying 20 results from an estimated 100000 matches similar to: "get mean of same elements in a data.frame"
2012 Dec 25
5
aggregate / collapse big data frame efficiently
Hi,
I need to aggregate rows of a data.frame by computing the mean for rows with the same factor-level on one factor-variable;
here is the sample code:
x <- data.frame(rep(letters,2), rnorm(52), rnorm(52), rnorm(52))
aggregate(x, list(x[,1]), mean)
Now my problem is, that the actual data-set is much bigger (120 rows and approximately 100.000 columns) ? and it takes very very long
2013 Jan 11
3
aggregate data.frame based on column class
Hi,
When using the aggregate function to aggregate a data.frame by one or more grouping variables I often have the problem, that I want the mean for some numeric variables but the unique value for factor variables.
So for example in this data-frame:
data <- data.frame(x = rnorm(10,1,2), group = c(rep(1,5), rep(2,5)), gender =c(rep('m',5), rep('f',5)))
aggregate(data,
2010 Feb 09
3
split strings in a vector and convert it to a data.frame
hi,
I have a vector full of strings like;
xy_100_ab xy_101_ab xy_102_ab xy_103_ab
I want to seperate each string in three pieces and the separator should be the "_"
at the end I want a data.frame like:
column1 column2 column3
xy 100 ab
xy 101 ab
xy 102 ab
xy 103 ab
I tried strsplit but I couldn't figure out how to convert the list I get into a data.frame.
I just
2011 Oct 05
2
mean of 3D arrays
Hi,
I have multiple three dimensional arrays.
Like this:
x1 <- array(rnorm(1000, 1, 2), dim=c(10, 10, 10))
x2 <- array(rnorm(1000, 1, 2), dim=c(10, 10, 10))
x3 <- array(rnorm(1000, 1, 2), dim=c(10, 10, 10))
Now I would like to compute the mean for each corresponding cell.
As a result I want to get one 3D array (10 x 10 x 10) in which at position x, y, z is the mean of the
2011 Jul 11
2
best way to aggregate / rearrange data.frame with different data types
Hi,
I have a data.frame that looks like this:
Subject <- c(rep(1,4), rep(2,4), rep(3,4))
y <- rnorm(12, 3, 2)
gender <- c(rep("w",4), rep("m",4), rep("w",4))
comment <- c(rep("comment A",4), rep("comment B",4), rep("comment C",4))
data <- data.frame(Subject,y,gender,comment)
data
Subject y gender
2011 Nov 09
2
algorithm that iteratively drops columns of a data-frame
Dear R-Users,
I have a problem with an algorithm that iteratively goes over a data.frame and exclude n-columns each step based on a statistical criterion.
So that the 'column-space' gets smaller and smaller with each iteration (like when you do stepwise regression).
The problem is that in every round I use a new subset of my data.frame.
However, as soon as I "generate" this
2012 Dec 19
2
create stratified splits
Hi,
I have a vector like:
r <- runif(100)
Now I would like to split r into 10 pieces (each with 10 elements) ?
but the 'pieces' should be roughly similar with regard to mean and sd.
what is an efficient way to do this in R?
thanks!
2011 Oct 11
2
replicate data.frame n times
Hi,
is there a way to replicate a data.frame like you can replicate the entries of a vector (with the repeat-function)?
I want to do this:
x <- data.frame(x, x)
(where x is a data.frame).
but n times.
And it should be as cpu / memory efficient as possible, since n is pretty big in my case.
thanks for any suggestions!
2011 Aug 22
2
test if vector contains elements of another vector (disregarding the position)
Hi,
I have the following problem:
I have two vectors:
i <- c('a','c','g','h','b','d','f','k','l','e','i')
j <- c('a', 'b', 'c')
now I would like to generate a vector with the length of i that
has zeros where i[x] != any element of j
and 1 where i[x] == any element of j.
2012 Jan 21
2
sequentially summing up a elements of a vector
Hi,
I have a somewhat abstract and tricky problem (at least for me).
Perhaps someone here can help me:
I would like to convert:
x <- c(0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0)
into
x <- c(0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4)
every 1 in x marks kind of a 'border' which is separated in different 'regions' in the
2012 Dec 18
7
Summarizing elements for a data.frame
Hello again, let say we have following data:
Dat1 <- structure(list(factor.sample.LETTERS.1.3...6..replace...T.. =
structure(c(1L,
3L, 2L, 1L, 3L, 3L), .Label = c("A", "B", "C"), class = "factor"),
factor.sample.letters.1.2...6..replace...T.. = structure(c(2L,
2L, 1L, 1L, 2L, 1L), .Label = c("a", "b"), class =
2008 May 02
2
Coercing by/tapply to data.frame for more than two indices?
Dear Colleagues,
Apologies for a long email to ask what I feel may be a very simple
question; I figure it's better to overspecify my situation.
I was asked a question, recently, by a colleague in my department
about pre-aggregating variables, i.e., computing the mean of defined subsets
of a data frame. Naturally, I thought of the 'by' and 'tapply' functions, as
2011 Oct 05
2
converting 3D array to a data-frame (with coordinate-columns x, y, z)
Hi,
I am still struggling with three dimensional arrays.
Now I would like to convert a three dimensional array into a data-frame with the coordinate-columns: x, y, z and a value-column.
And I definitely don't want to loop over every element, since this would be very resource intensive for the actual data-set.
Are there any specific functions that are helpful for this task?
2013 Feb 26
4
cut a vector in equal parts
Hi,
I would like to cut a vector of values in parts.
Each part should have an equal number of elements.
for example:
x <- (rnorm(500)^2)
now I want 5 vectors each with 100 elements.
The first vector should include the 100 lowest values of x and so on
(so that the fifth vector contains the 100 highest values of x).
thanks for any help!
2005 Jul 25
2
how to get the group mean deviation data ?
> n=10;t=3
> d<-cbind(id=rep(1:n,each=t),y=rnorm(n*t),x=rnorm(n*t),z=rnorm(n*t))
> head(d)
id y x z
[1,] 1 -2.1725379 0.07629954 -0.3985258
[2,] 1 -1.2383038 -2.49667038 0.6966127
[3,] 1 -1.2642401 -0.50613307 0.4895856
[4,] 2 0.2171246 0.86711864 -0.6660036
[5,] 2 2.2765760 -0.48547142 -1.4496664
[6,] 2 0.5985345 -1.06427035 2.1761071
2011 Jan 31
2
From data frame to list object
Dear all, let say I have following data frame:
> data.frame(x=rnorm(18), y=rep(c("a", "b", "c"), each=6))
x y
1 -1.072152537 a
2 0.382985265 a
3 0.058877377 a
4 -0.006911939 a
5 -2.355269051 a
6 -0.303095553 a
7 0.484038422 b
8 0.733928931 b
9 -1.136014346 b
10 0.503552090 b
11 1.708609658 b
12 -0.294599403 b
13
2009 Jun 18
3
merging data.frames of different length
hi,
I have two data.frames each with two columns;
x1
1 4
1 3
1 6
2 9
2 2
2 5
3 6
3 7
3 4
x2
1 -3
1 -7
2 -3
2 -2
2 -8
3 -1
3 -2
3 -1
now I want to merge this data.frames to one data.frame.
The problem is, that sometimes there is a different number of elements
per category.
(like above x1 has 3 values for the value 1 in the first row, but x2
has only 2 values for the value 1 in the
2011 Oct 10
1
calculate multiple means of one vector
Dear R-Users,
I have the following two vectors:
data <- rnorm(40, 0, 2)
positions <- c(3, 4, 5, 8, 9, 10, 20, 21, 22, 30, 31, 32)
now I would like to calculate the mean of every chunk of data-points (of the data-vector) as defined by the positions-vector.
So I would like to get a vector with the mean of element 3 to 5 of the data-vector, 8 to 10, 20 to 22 and so
2012 Jan 29
2
apply lm() to each row of a matrix
Hi,
I would like to fit lm-models to a matrix with 'samples' of a dependent variable (each row represents one sample of the dependent variable).
The independent variable is a vector that stays the same:
y <- c(1:10)
x <- matrix(rnorm(5*10,0,1), 5, 10)
now I would like to avoid looping over the rows, since my original matrix is much larger;
for(t in 1:dim(x)[1]) {
2010 Oct 07
3
Create 2d table with mean of entries
Hi,
I have the following problem:
I have a list of entries with vehicles going from some places to others and
the time that they need.
e.g.
Vehicle Start End Time
1 A B 5
2 A C 4
3 A C 3
4 B A 6
5 B C 4
6 B C 6
7 C B 2
8 C B 4
9