1

A: sum at different levels

 2 years ago
source link: https://www.codesd.com/item/a-sum-at-different-levels.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

A: sum at different levels

advertisements

This question already has an answer here:

  • generating sums of data according to values of a variable 3 answers

I have a dataset X as:

customer_id event_type tot_count
931 1 5
231 2 6
231 1 3
333 3 9
444 1 1
931 3 3
333 1 21
444 2 43

I need a sum at customer_id and event_type level. This is a 1 line code in SQL as:

select customer_id, event_type, sum(tot_count) from X group by 1,2

I need the same operation in R.


You can use the aggregate function:

aggregate(tot_count ~ customer_id + event_type, X, sum)

 customer_id event_type tot_count
1         231          1         3
2         333          1        21
3         444          1         1
4         931          1         5
5         231          2         6
6         444          2        43
7         333          3         9
8         931          3         3

Related Articles

Different levels on unit play

I am planning to make a android game using unity game engine. I would like to include different levels as we see in angry birds and some other games. My question is whether I have to build different scenes for each levels. I am completely new to this

Detect different levels in column

I do have a column with about 80k entries which has only 22 different levels (the number of the chromosome). Is there any quick trick in R to find out at which position a level changes into the next ... so to figure out at which row chromosome 1 chan

In Winston Logging Module, why sending logs with different levels does not work

In my node application i'm using winston module to store my application logs.I have tried to store the logs in different level and also in different files.In this situation i'm getting error as "Error: Transport already attached: file". My Code

logback: two applications, multiple recorders, different levels

I want to have two log files in my application (Spring Integration), debug.log and main.log. I want to run main.log at an INFO level and debug.log at a DEBUG level. This is doable with filters on the appenders. I want to log different levels to the a

How to stop AdvancedDataGrid with HierarchicalData from moving child elements with multiple parents at different levels?

I'm having a problem where my child element is moved together with itself in the chart where it has multiple parents at different levels. This is kind of odd and I think it might be intended functionality to illustrate that it's the same element but

R - Using the If statement to compare factors with different levels

I would like to compare two columns in my dataset, however they have different levels. I cant seem to find a way to get this to work. Any suggestions? Example: x = c('a','b','c') y = c('a','b','g') z = data.frame(x,y) if(z$x == z$y){1} else{0} return

are there different levels of synchronization?

I have heard that there are different levels of Synchronization.are there?(If there are ,would you please explain them with a snippet code?)thanks.Before Java 5 there was only one: the synchronized keyword. This waited for and obtained an exclusive l

How to set different levels for different python log managers

I've read a few posts on this but I'm still confused. I have this logging setup: import logging class MongoHandler(logging.Handler): def __init__(self): logging.Handler.__init__(self) from pymongo import Connection self.db = Connection('db_server').d

C ++ friends class of the same name in different levels of a nested namespace

Not sure if this is possible, but I have two classes of the same name in different levels of a nested namespace and I'd like to make the more shallow class a friend of the deeper class. Example: In File1.h: namespace A { class Foo { //stuff }; } In F

xpath displays node data at different levels

<?xml version="1.0"?> -<bookstore> <book > <title>aaaa</title> -<author > <first-name>firts</first-name> <last-name>last</last-name> </author> <price>8.23</price> <

Sum of different columns in a data.frame

I have a very big data.frame and want to sum the values in every column. So I used the following code: sum(production[,4],na.rm=TRUE) or sum(production$X1961,na.rm=TRUE) The problem is that the data.frame is very big. And I only want to sum 40 certai

Number of sublists in a list, with equal sum, from odd levels in Lisp

I have a non-linear list. I need to find out the number of sub-lists at any level in the initial list, for which the sum of the numerical atoms at the odd levels, is an even number. The superficial level is counted as 1. I wrote something like: (defu

Different levels of access for users using PHP & amp; MySQL

I am developing a website for testing with logging page with different access levels for users, the site is connected with MySQL db which have 3 columns user | password | level(1/2) code is filter out the particular entry from db but it didn't redire

MySQL sum giving different values

I have a table with a list of payments. When I try to retrieve the total I get different values depending on which columns I use in the SUM(). Here is my query: SELECT sum(Email_Broadcasting+Custom_Email_Newsletter+Digital_Newsletter+Mortgage_Matters

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK