Calculation by group in one column R [duplicate] - r

This question already has answers here:
Adding a column of means by group to original data [duplicate]
(4 answers)
Closed 6 years ago.
group=c("A","A","B","A","B","C","C","A")
y=c(3,4,5,2,1,4,1,2)
df=data.frame(group,y)
using aggregate, I can get the average by
aggregate(df$y, list(df$group), mean)
But my question is: How to do something like : (y_ij-mean_i)
where mean_i is the average for group i
thank you.

We can use ave
with(df, y- ave(y, group))

Related

Calculate the sum of obervation values with conditions [duplicate]

This question already has answers here:
Grouping functions (tapply, by, aggregate) and the *apply family
(10 answers)
Closed 2 years ago.
The given data is like this:
df<-data.frame(farmer=c("F1","F1","F1","F2","F2","F2","F3","F4","F4"),
c2=c(4,4,5,3,3,3,1,2,1))
df
Question is: I would like to get the sum of farmer whose values in the c2 column are the same.
The output of this problem is 2. But how to use code to realize it? Which function should be used?
You can try the code below
nrow(unique(subset(df,ave(c2,farmer)==c2)))
where subset+ ave filters out the farmers who have identical c2 values, and nrow + unique helps count the number of those farmers.

R sum on rows based on day [duplicate]

This question already has answers here:
How to sum a variable by group
(18 answers)
Closed 5 years ago.
I have a dataframe containing sells by day, but as there are many values for one day, so i'd like to rebuilt my dataframe to have just one value per day which is the sum of all the sells of this day.
what i have:
what i'd like to have:
(the values are not well calculated but it's for the example)
I tried agregate and functions like this but it dosn't work and I dont know how to do this...
Thanks for help
Aggregate should work
aggregate(TOT_OP_MAIN_PAID_MNT,by=list(DATE_ID),FUN=sum)
This should work
df <- data.frame(DATE_ID = 1, TOT_OP_MAIN_PAID_MNT = 1)
aggregate(TOT_OP_MAIN_PAID_MNT ~ DATE_ID, df, sum)

The most efficient way to calculate numbero of unique values in vector [duplicate]

This question already has answers here:
How to count the number of unique values by group? [duplicate]
(1 answer)
Unique values in each of the columns of a data frame
(9 answers)
Closed 5 years ago.
Is there better way to compute this instead of:
length(unique(vector))
let's assume that we donno what class is the vector.
You can use
library(data.table)
vector <- c(1,2,2,2,3)
uniqueN(vector)

How can I group by name in R and apply sum to the other 2 columns? [duplicate]

This question already has answers here:
How to sum a variable by group
(18 answers)
Closed 6 years ago.
I am trying to summarize this dataset by grouping by name (Almeria, Ath Bilbao,...) and have the sum of its corresponding values in column 2 (HalfTimeResult) and 3 (FullTimeResult). I tried with the aggregate and group_by functions but have not been able to obtain the right output.
What function and how would I use it to obtain an output like this?
This is the dataset that I am working with:
We can use data.table
library(data.table)
setDT(df1)[, lapply(.SD, sum), by = HomeTeam]

R- Display value with aggregate [duplicate]

This question already has answers here:
R use ddply or aggregate
(4 answers)
Aggregate by factor levels, keeping other variables in the resulting data frame
(5 answers)
Closed 9 years ago.
I have a dataframe in R with the following variables:
DateTime year dayYear diameter dendro
I want to calculate the min diameter for each day of the year for each dendrometer, so I used aggregate:
dailymin <- aggregate(diameter~year+dayYear+dendro, FUN=min, data=myData)
However, I also need the time when the min diameter happened at each day and dendro. Therefore, I need to keep the info contained in the variable DateTime in the row of each min.
Thanks for helping me.
You may alternatively use the plyr package for this purpose and add a new column by the transform shortcut function:
ddply(myData, .(variables.list), transform, new.variable= min(diameter))

Resources