Hi everyone,
I have the following dataframe:
structure(list(Department = c("A", "A", "A", "A", "A", "A", "A",
"A", "B", "B", "B", "B", "B", "B", "B", "B"), Class = c(1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), Value = c(0L,
100L, 800L, 800L, 0L, 300L,
Hello, Elahe,
you were, of course, supposed to insert my suggested
code-snippet into you code and test it therein ...
Regards -- Gerrit
-
Dr. Gerrit Eichner Mathematical Institute, Room 212
gerrit.eich...@m
Hello Gerit
mutate(MinValue = min(Value[Value != 0]) ) or mutate(MinValue =
sort(unique(Value))[2]) only mutates one value which is 100, it doesnt mutate
minimum Value != 0 per group by element
On Tuesday, May 11, 2021, 01:26:49 PM GMT+2, Gerrit Eichner
wrote:
Homework?
Try ma
Hello,
This can be done by getting the min of Value[Value != 0].
In the code that follows I have named the expected output df2 and
assigned the result to df3 and df4.
library(dplyr)
df3 <- df %>%
group_by(Department,Class) %>%
mutate(flag = Value != 0,
MinValue = min(Value[flag]) ) %
Homework?
Try maybe
mutate(MinValue = min(Value[Value != 0]) )
or
mutate(MinValue = sort(unique(Value))[2])
Hth -- Gerrit
-
Dr. Gerrit Eichner Mathematical Institute, Room 212
gerrit.eich...@math.uni-gie
Hi all,
I have the following data frame
dput(df)
structure(list(Department = c("A", "A", "A", "A", "A", "A", "A",
"A", "B", "B", "B", "B", "B", "B", "B", "B"), Class = c(1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), Value = c(0L,
100L, 800L, 800L, 0L, 300L, 1200L, 1200
Hi carol,
You could use the "I" function, which will just return what you pass to it.
Jim
On Thu, Mar 10, 2016 at 12:28 AM, carol white via R-help
wrote:
> What should be FUN in aggregate as no function like mean, sum etc will be
> applied
> Carol
>
> On Wednesday, March 9, 2016 1:59 PM, S
On Wed, Mar 9, 2016 at 8:28 AM, carol white wrote:
> What should be FUN in aggregate as no function like mean, sum etc will be
> applied
I have no idea, since you haven't told us what you want the results to
look like.
> Carol
>
>
> On Wednesday, March 9, 2016 1:59 PM, Sarah Goslee
> wrote:
>
gt; From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of carol
> white via R-help
> Sent: Wednesday, March 09, 2016 2:28 PM
> To: Sarah Goslee
> Cc: R-help Help
> Subject: Re: [R] group by rows
>
> What should be FUN in aggregate as no function like mean, sum e
> How is it possible to group rows of a matrix or a data frame by the same
> values
> of the first column?
If you mean _group_ as in SQL GROUP BY, use aggregate() with a count or summary
statistic.
If you mean _sort_, just to get similar values close together, use order()
For example, to sort
What should be FUN in aggregate as no function like mean, sum etc will be
applied
Carol
On Wednesday, March 9, 2016 1:59 PM, Sarah Goslee
wrote:
Possibly aggregate(), but you posted in HTML so your data were mangled.
Please use dput(), post in plain text, and try to explain more clear
Possibly aggregate(), but you posted in HTML so your data were mangled.
Please use dput(), post in plain text, and try to explain more clearly
what you want the result to look like.
Sarah
On Wed, Mar 9, 2016 at 7:09 AM, carol white via R-help
wrote:
> How is it possible to group rows of a matri
How is it possible to group rows of a matrix or a data frame by the same values
of the first column?
1 14331 453452 653 3762 45
1 1433,453452 45, 653 376
Thanks
Carol
[[alternative HTML version deleted]]
__
R-help@r-project.org mailing list
many thanks for replying,
yes I am kindly accept my pardon, I mistaken the rows from each other many
thanks I will read them.
Ragia
> Date: Sat, 7 Nov 2015 07:48:36 -0800
> Subject: Re: [R] group by function
> From: bgunter.4...@gmail.com
&
??
Row 4 has i = 2 and Measure_id =4 and therefore has value divided by
the max of all values with that i and Measure_id, which is 1. Row 7
has i =2 and Measure_id =2, and so is divided by the max value in all
rows with those values of i and Measure_id, which is 2. etc. So either
you do not unders
Dear group,
kindly, I have the following data frame
structure(c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2,
3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1.5, 2, 1, 0, 2, 2, 1.5,
0, 0, 1, 1, 2, 0, 1, 2), .Dim = c(15L, 3L), .Dimnames = list(
NULL, c("i", "Measure_id", "value")))
it has 3 c
yes thanks, that's correct!
here a slight variation inspired by your solution: a cartesian product
restricted to non duplicated records to get the logical vector i to be
used in the next natural join
i<-!duplicated(merge(df1$id,df1$item, by=NULL))
merge(df1[i,],df2)
thanks
Il 08/05/2014 18:
Hi,
May be:
indx <- !duplicated(as.character(interaction(df1[,-3])))
merge(df1[indx,],df2)
A.K.
On Thursday, May 8, 2014 12:34 PM, Massimo Bressan
wrote:
yes, thank you for all your replies, they worked out correctly indeed...
...but because of my fault, by then working on my real data I ful
yes, thank you for all your replies, they worked out correctly indeed...
...but because of my fault, by then working on my real data I fully
realised that I should have mentioned something that is changing (quite
a lot, in fact) the terms of the problem...
please would you consider the follow
Hi,
May be this helps:
merge(unique(df1),df2)
A.K.
On Thursday, May 8, 2014 5:46 AM, Massimo Bressan
wrote:
given this "bare bone" example:
df1 <- data.frame(id=rep(1:3,each=2), item=c(rep("A",2), rep("B",2),
rep("C",2)))
df2 <- data.frame(id=c(1,2,3), who=c("tizio","caio","sempronio"))
Hello,
There are some alternatives without using sqldf or another package.
1.
tmp2 <- aggregate(item ~ id, data = df1, FUN = unique)
Then merge() like you've done.
2.
tmp3 <- merge(df1, df2)
tmp3[!duplicated(tmp3), ]
Hope this helps,
Rui Barradas
Em 08-05-2014 10:44, Massimo Bressan escr
given this "bare bone" example:
df1 <- data.frame(id=rep(1:3,each=2), item=c(rep("A",2), rep("B",2),
rep("C",2)))
df2 <- data.frame(id=c(1,2,3), who=c("tizio","caio","sempronio"))
I need to group the first dataframe "df1" by "id" and then merge with
the second dataframe "df2" (again by "id")
entical(datTest2,as.data.frame(dtTest2))
#[1] TRUE
A.K.
- Original Message -
From: Michael Liaw
To: r-help@r-project.org
Cc:
Sent: Saturday, August 3, 2013 8:11 PM
Subject: [R] Group by a data frame with multiple columns
Hi
I'm trying to manipulate a data frame (that has about 10
Hi
I'm trying to manipulate a data frame (that has about 10 million rows) rows
by "grouping" it with multiple columns. For example, say the data set looks
like:
Area
Sex
Year
y
Bob
F
2011
1
Bob
F
2011
2
Bob
F
2012
3
Bob
M
2012
3
Bob
M
2012
2
Fred
F
2011
1
Fr
On Mon, 30 May 2011 16:47:45 -0500,
"Mendolia, Franco" wrote:
> Hello, I would like to create a group variable that is based on the
> values of three variables:
> For example,
>> dat <- data.frame(A=c(1,1,1,1,1,2,2,2,2,2),
> B=c(1,1,1,5,5,5,9,9,9,9),
> C=c(1,1,1,1,1,2,2,7,7,
Hi,
There are probably much better ways, but try this
transform(dat, group = as.numeric(factor(paste(A,B,C, sep=""
HTH,
baptiste
On 31 May 2011 09:47, Mendolia, Franco wrote:
> Hello,
>
> I would like to create a group variable that is based on the values of three
> variables:
>
> For ex
Hello,
I would like to create a group variable that is based on the values of three
variables:
For example,
> dat <- data.frame(A=c(1,1,1,1,1,2,2,2,2,2),
B=c(1,1,1,5,5,5,9,9,9,9),
C=c(1,1,1,1,1,2,2,7,7,7))
> dat
A B C
1 1 1 1
2 1 1 1
3 1 1 1
4
ok, i have it - match()
10x all again! :)
--
View this message in context:
http://r.789695.n4.nabble.com/group-by-in-data-frame-tp3324240p3325269.html
Sent from the R help mailing list archive at Nabble.com.
__
R-help@r-project.org mailing list
http
hi guys,
many many thanks for all the solutions! :-D they are working great!
i have another "little" question:
i would like to save these groups in a new column with serial number like
the solution from David, but wit integer values: 1,2,3...
i do this allready but with my 1. solution and there
Hi:
Here's another way:
c1<-c(1,2,3,2,2,3,1,2,2,2)
c2<-c(5,6,7,7,5,7,5,7,6,6)
c3<-rnorm(10)
x <- data.frame(c1 = factor(c1), c2 = factor(c2), c3)
x <- transform(x, mean = ave(c3, c1, c2, FUN = mean))
Yet another with function ddply() in package plyr:
ddply(x, .(c1, c2), transform, mean = mean(c3
On Feb 25, 2011, at 10:14 AM, zem wrote:
Yeah, you are right
i want to post an short example what i want to do .. and in the
meantime i
solved the problem ...
but here is:
i have something like this dataframe:
c1<-c(1,2,3,2,2,3,1,2,2,2)
c2<-c(5,6,7,7,5,7,5,7,6,6)
c3<-rnorm(10)
x<-cbind(c1,c
Ok, now I think I've understood, but I'm not sure since I think that my
ave() solution does work. Although, I though you have several numerical
variables and 1 factor; it is the opposite but it is still possible:
c3_mean <- ave(x[,3], list(x[,1],x[,2]), FUN=mean) #note that values
are differe
Yeah, you are right
i want to post an short example what i want to do .. and in the meantime i
solved the problem ...
but here is:
i have something like this dataframe:
c1<-c(1,2,3,2,2,3,1,2,2,2)
c2<-c(5,6,7,7,5,7,5,7,6,6)
c3<-rnorm(10)
x<-cbind(c1,c2,c3)
> x
c1 c2 c3
[1,] 1 5
10x i solved it ... mein problem was that i had 2 column by them i have to
group, i just "pasted" the values together so that at the end i have one
column to group and then was easy ...
here is the script that i used:
http://tolstoy.newcastle.edu.au/R/help/06/07/30184.html
Ivan thanks for the hel
Hi Ivan,
thanks for your replay!
but the problem is there that the dataframe has 2 rows and ca. 2000
groups, but i dont have the column with the groupnames, because the groups
are depending on 2 onother columns ...
any other idea or i didnt understand waht are you posted ... :(
--
View th
Hi,
I think ave() might do what you want:
df <- data.frame(a=rep(c("this","that"),5), b1=rnorm(10), b2=rnorm(10))
ave(df[,2], df[,1], FUN=mean)
For all columns, you could do that:
d <- lapply(df[,2:3], FUN=function(x)ave(x,df[,1],FUN=mean))
df2 <- cbind(df, d)
HTH,
Ivan
Le 2/25/2011 12:11, zem
Hi all,
i have a little problem, and i think it is really simple to solve, but i
dont know exactly how to.
here is the "challange":
i have a data.frame with n colum, i have to group 2 of them and calculate
the mean value of the 3. one. so far so good, that was easy - i used
aggregate function to
My point is this:
if we do
temp<-tDates
aggregate(DF[,c('V2', 'V3')],
list(format(temp, "%H:%M:00")),
FUN = sum)
Does agrregate still know it's refering to DF$V1 - and if so- how?
--
View this message in context:
http://n4.nabble.com/Group-by-tp1588694p1589600.html
Se
tDates assumes DF$V1, which is from DF object.
On Thu, Mar 11, 2010 at 11:13 AM, ManInMoon wrote:
>
> Thank you
>
> In the "aggregate" how does it know which column to group?
>
> I know you give it tDates as the second argument, but it doesn't know that
> is from DF - we could have put anything t
Thank you
In the "aggregate" how does it know which column to group?
I know you give it tDates as the second argument, but it doesn't know that
is from DF - we could have put anything there I think...
--
View this message in context:
http://n4.nabble.com/Group-by-tp1588694p1589005.html
Sent fr
> DF
V1 V2 V3
1 10:03:13 3.4 1002
2 10:03:14 5.6 1001
3 10:05:27 7.2 999
4 10:05:33 8.2 998
> DF2 <- t(sapply(split(DF[,-1], gsub('(.{5}).*', '\\1:00', DF$V1)), colSums))
> data.frame(V1 = rownames(DF2), DF2)
V1 V2 V3
10:03:00 10:03:00 9.0 2003
10:05:00 10:05:00 15.
Try this:
DF <- read.csv(textConnection("V1,V2,V3
10:03:13,3.4,1002
10:03:14,5.6,1001
10:05:27,7.2,999
10:05:33,8.2,998"), header = TRUE)
tDates <- strptime(DF$V1, "%H:%M:%S")
tDates[[2]] <- floor(strptime(DF$V1, "%H:%M:%S")[[2]] / 5) * 5
aggregate(DF[,c('V2', 'V3')],
list(format(tDate
I have a matrix with a POSIXct as a numeric in the first column.
I would like to create a new matrix that is "grouped by" my chosed time
bars.
i.e. So I would like to group by hour or day or 5 days, and have all my
columns be summed or averaged or counted..
mydata:
V1,V2,V3
10:03:13,3.4,1002
1
On Mon, Apr 13, 2009 at 8:56 AM, Nick Angelou wrote:
>
>> data
> X1 X2 X3 X4
> 1 1 2 2 1
> 2 1 1 2 2
> 3 1 1 2 2
> 4 2 2 1 2
> 5 1 1 2 2
> 6 2 2 1 2
> 7 1 1 2 1
> 8 2 2 1 2
> 9 1 2 1 1
> 10 1 1 2 2
>
> sqldf("select X1, X2, X3, X4, count(*) CNT fr
Assuming DF is your data frame try this: ftable(DF)
In SQL you can get close with:
sqldf("select X1, X2, X3, sum(X4 == 1) `X4=1`, sum(X4 == 2) `X4=2`
from DF group by X1, X2, X3 order by X1, X2, X3")
On Mon, Apr 13, 2009 at 9:56 AM, Nick Angelou wrote:
>
>
> Gabor Grothendieck wrote:
>>
>> SQL
Gabor Grothendieck wrote:
>
> SQL has the order by clause.
>
Gabor, thanks for the suggestion. I thought about this but ORDER BY cannot
create the tabular structure that I need. Here is more detail about my
setting:
f1, f2, f3 have unique triplets (each repeating a different number of
times).
On Apr 13, 2009, at 7:26 AM, Nick Angelou wrote:
Thanks a lot, guys. Gabor's and Mike's suggestion worked. Duncan's
did not do
exactly what I expected (I guess it's the "paste" in Mike's that makes
"table" work as I needed it).
One more question - is there a convenient way to order the gro
SQL has the order by clause.
On Mon, Apr 13, 2009 at 7:26 AM, Nick Angelou wrote:
>
> Thanks a lot, guys. Gabor's and Mike's suggestion worked. Duncan's did not do
> exactly what I expected (I guess it's the "paste" in Mike's that makes
> "table" work as I needed it).
>
> One more question - is t
Thanks a lot, guys. Gabor's and Mike's suggestion worked. Duncan's did not do
exactly what I expected (I guess it's the "paste" in Mike's that makes
"table" work as I needed it).
One more question - is there a convenient way to order the group by results
as follows:
As rows: the unique combinati
Mike Lawrence wrote:
One way:
g= paste(f1,f2,f3,f4)
table(g)
I'd go for
g <- interaction(f1,f2,f3,f4, drop=TRUE)
table(g)
which is essentially the same thing.
On Mon, Apr 13, 2009 at 7:33 AM, Nick Angelou wrote:
Hi,
I have the following table data:
f1, f2, f3, f4.
I want to compute t
One way:
g= paste(f1,f2,f3,f4)
table(g)
On Mon, Apr 13, 2009 at 7:33 AM, Nick Angelou wrote:
>
> Hi,
>
> I have the following table data:
>
> f1, f2, f3, f4.
>
> I want to compute the counts of unique combinations of f1-f4. In SQL I would
> just write:
>
> SELECT COUNT(*) FROM GROUP BY f1, f2,
You can use SQL commands directly on R data frames with the R sqldf package:
See home page:
http://sqldf.googlecode.com
On Mon, Apr 13, 2009 at 6:33 AM, Nick Angelou wrote:
>
> Hi,
>
> I have the following table data:
>
> f1, f2, f3, f4.
>
> I want to compute the counts of unique combinations of
Nick Angelou wrote:
Hi,
I have the following table data:
f1, f2, f3, f4.
I want to compute the counts of unique combinations of f1-f4. In SQL I would
just write:
SELECT COUNT(*) FROM GROUP BY f1, f2, ..,f4.
How to do this in R?
table(f1,f2,f3,f4) will give you the counts.
Other statistic
Hi,
I have the following table data:
f1, f2, f3, f4.
I want to compute the counts of unique combinations of f1-f4. In SQL I would
just write:
SELECT COUNT(*) FROM GROUP BY f1, f2, ..,f4.
How to do this in R?
Thanks,
Nick
--
View this message in context:
http://www.nabble.com/Group-by-in-
Luc, Eik, Jorge,
thanks to all of you!
Nice to see how many different solutions there are for the same
problem. :)
Best
Henning
Am 07.04.2009 um 22:25 schrieb Jorge Ivan Velez:
>
> Dear Hans,
>
> Try also:
>
> x <- structure(c(2, 1, 2, 1, 3, 2, 4, 1, 4, 3, 4, 3, 5, 2, 5, 1, 6,
> 1), .Dim
Dear Hans,
Try also:
x <- structure(c(2, 1, 2, 1, 3, 2, 4, 1, 4, 3, 4, 3, 5, 2, 5, 1, 6, 1), .Dim
= c(2L, 9L))
tapply(x[2,],x[1,],sum)
#2 3 4 5 6
#2 2 7 3 1
HTH,
Jorge
On Tue, Apr 7, 2009 at 11:06 AM, Hans-Henning Gabriel <
hanshenning.gabr...@gmail.com> wrote:
> Hi,
>
> my problem is as foll
bycols.df , FUN = sum,
> keep.names = TRUE )
> sumbycols.df
x y
1 2 2
2 3 2
3 4 7
4 5 3
5 6 1
> sumbyrows.df <- t ( sumbycols.df )
> sumbyrows.df
1 2 3 4 5
x 2 3 4 5 6
y 2 2 7 3 1
>
- Original message -
From: "Eik Vettorazzi"
To: "Hans-Henning Ga
see ?aggregate
m<-rbind(sample(2:6,9,replace=T),sample(1:3,9,replace=T))
aggregate(m[2,],by=list(m[1,]),sum)
Hans-Henning Gabriel schrieb:
Hi,
my problem is as follows:
I have a matrix of two rows like this:
2 2 3 4 4 4 5 5 6
1 1 2 1 3 3 2 1 1
Can I apply something like "group by" in sql? W
Hi Henning,
Although there might be a better way to do this, I'd suggest (with
my.matrix being the matrix you provided):
complete.vector = rep(my.matrix[1,],my.matrix[2,]) ;
table(complete.vector) ;
Is this what you're looking for?
Cheers,
Luc
Hans-Henning Gabriel wrote:
Hi,
my problem i
Hi,
my problem is as follows:
I have a matrix of two rows like this:
2 2 3 4 4 4 5 5 6
1 1 2 1 3 3 2 1 1
Can I apply something like "group by" in sql? What I want to achieve
is the some of second row for each unique entry of first row:
2 -> 2 (=1+1)
3 -> 2
4 -> 7 (=1+3+3)
5 -> 3 (=2+1)
6 -
The gtools package has quantcut.
On Tue, Nov 18, 2008 at 6:53 AM, Daniel Brewer <[EMAIL PROTECTED]> wrote:
> Hello,
>
> I was just wondering whether there is a quick way to divide a vector of
> data into four groups defined by the quantiles?
> i.e.
> 0-25%
> 25-50%
> 50-75%
> 75-100%
>
> Many than
Daniel Brewer wrote:
Hello,
I was just wondering whether there is a quick way to divide a vector of
data into four groups defined by the quantiles?
i.e.
0-25%
25-50%
50-75%
75-100%
Many thanks
Dan
library(Hmisc)
cut2(x, g=4)
--
Frank E Harrell Jr Professor and Chair School of M
check at cut() and split(), e.g.,
x <- rnorm(100)
qx <- quantile(x)
ind <- cut(x, qx, include.lowest = TRUE)
split(x, ind)
I hope it helps.
Best,
Dimitris
Daniel Brewer wrote:
Hello,
I was just wondering whether there is a quick way to divide a vector of
data into four groups defined by the
Hello,
I was just wondering whether there is a quick way to divide a vector of
data into four groups defined by the quantiles?
i.e.
0-25%
25-50%
50-75%
75-100%
Many thanks
Dan
--
**
Daniel Brewer, Ph.D.
Institute of Cancer Research
M
Try this also;
xtabs(volume ~ day, data = x)
On Wed, Oct 1, 2008 at 10:58 AM, Max Rausch <[EMAIL PROTECTED]> wrote:
> I have a data frame with the following information
>
> day hourvolume
> 1 2003-07-18 10 836700
> 2 2003-07-18 11 375000
> 3 2003-07-18 12
On Wed, Oct 1, 2008 at 8:58 AM, Max Rausch <[EMAIL PROTECTED]> wrote:
> I have a data frame with the following information
>
> day hourvolume
> 1 2003-07-18 10 836700
> 2 2003-07-18 11 375000
> 3 2003-07-18 12 6
> 4 2003-07-188 102
> 5
Two approaches
?aggregate or the reshape package
aggregate(xx$volume, list(day=xx$day), sum)
library(reshape)
names(xx)[3] <- "value"
cast(xx, day ~ ., sum)
--- On Wed, 10/1/08, Max Rausch <[EMAIL PROTECTED]> wrote:
> From: Max Rausch <[EMAIL PROTECTED]&
tapply(data$volume, data$day, sum)
would do I think. But I haven't tried, was to lazy to type in some
data, I'm sorry.
Gabor
On Wed, Oct 1, 2008 at 3:58 PM, Max Rausch <[EMAIL PROTECTED]> wrote:
> I have a data frame with the following information
>
> day hourvolume
> 1 2003
?aggregate
day hour volume
1 2003-07-18 10 836700
2 2003-07-18 11 375000
3 2003-07-18 12 6
4 2003-07-188 102
5 2003-07-189 39
> aggregate(x$volume, list(x$day), sum)
Group.1 x
1 2003-07-18 2681700
On Wed, Oct 1, 2008 at 9:58 AM, Max Rausch <[EM
I have a data frame with the following information
day hourvolume
1 2003-07-18 10 836700
2 2003-07-18 11 375000
3 2003-07-18 12 6
4 2003-07-188 102
5 2003-07-189 39
I have been trying create a new data frame with the
70 matches
Mail list logo