Calculate sum column filtering identical values on multiple columns

I have data for multiple columns (S1,S2,S3) and I’m trying to create a sum column (result). I want to sum values for each row that has identical values in S1, S2 and S3 columns. Here is a sample data and the result I’m looking for. S1 <- c(1,1,1,0,1,0) S2 <- c(1,1,1,0,1,0) S3 <- c(1,0,0,0,0,0) value… Read More Calculate sum column filtering identical values on multiple columns

Error on creating conditional column in R

Thank you for your kind responses. I have a dataframe and I need to apply a formula over a columns depending of the value of other column: data <- data.frame(figure = c("square","square","circle","square","circle"), diameter =c(NA,NA,21,NA,12), side=c(32,27,NA,51,NA)) What I need is to calculate the area according the square or circle formula (square = side * side, circle=… Read More Error on creating conditional column in R

Calculating row sums in data frame based on column names

I have a data frame with media spending for different media channels: TV <- c(200,500,700,1000) Display <- c(30,33,47,55) Social <- c(20,21,22,23) Facebook <- c(30,31,32,33) Print <- c(50,51,52,53) Newspaper <- c(60,61,62,63) df_media <- data.frame(TV,Display,Social,Facebook, Print, Newspaper) My goal is to calculate the row sums of specific columns based on their name. For example: Per definition Facebook… Read More Calculating row sums in data frame based on column names

Django count and store values in models

I have multiple Models which look like this class Classes(models.Model): User = models.ForeignKey(User, on_delete=models.SET_NULL, null=True) A1 = models.IntegerField(default=0) B1 = models.IntegerField(default=0) etc A2 = models.IntegerField(default=0) B2 = models.IntegerField(default=0) etc A3 = models.IntegerField(default=0) B3 = models.IntegerField(default=0) etc A4 = models.IntegerField(default=0) B4 = models.IntegerField(default=0) etc Sum_of_1 = models.IntegerField( blank=True, null=True) Sum_of__2 = models.IntegerField( blank=True, null=True) Sum_of__3 =… Read More Django count and store values in models

Creating A New Calculated Category Within A Column in R

Suppose I have a data frame similar to this, only with 1000’s of observations: df <- data.frame(Group = c(‘A’, ‘A’, ‘A’, ‘B’, ‘B’, ‘B’,’B’,’C’,’C’,’C’,’D’,’D’,’D’,’D’,’D’), Values=c(‘5′,’7′,’9′,’0′,’8′,’4′,’5′,’2′,’1′,’3′,’6′,’3′,’1′,’3′,’5’)) What I want to do is add a new calculated group to the data frame based on values in a group that already exists in the data frame without replacing… Read More Creating A New Calculated Category Within A Column in R

How to calculate the persondays in a year based on two date columns

I would like to count the amount of days an ID/row(person) has during a period of 1 year. Below an example of my dataset. (Inschrijfdatum = registration date, Uitschrijfdatum = deregistration date) Inschrijfdatum Uitschrijfdatum <date> <date> 1 1996-02-22 2019-01-11 2 2011-10-31 2019-02-25 3 1992-06-15 2019-03-10 4 2001-11-13 2022-01-01 5 2019-02-18 2019-09-07 6 2019-12-30 2022-01-01 #… Read More How to calculate the persondays in a year based on two date columns

How to create and populate pandas columns based on cell values

I have created a dataframe called df as follows: import pandas as pd d = {‘feature1’: [1, 22,45,78,78], ‘feature2’: [33, 2,2,65,65], ‘feature3’: [100, 2,359,87,2],} df = pd.DataFrame(data=d) print(df) The dataframe looks like this: I want to create two new columns called Freq_1 and Freq_2 that count, for each record, how many times the number 1… Read More How to create and populate pandas columns based on cell values

How to calculate the summation for values based on consecutive days and two other columns

How can I do summation just for consecutive days and for the same name and same supplier? For instance, for A and Supplier Wal, I need to do summation for 2021-05-31 and 2021-06-01 and then do another summation for 2021-06-08 and 2021-06-09. I need to add a new column for summation. Please take a look… Read More How to calculate the summation for values based on consecutive days and two other columns