简体   繁体   中英

How to add values from 2 rows together based on 2 conditions R.?

So under category, there's category c and category c-ETS. I want add values for both rows together and return the value under category c if the year and month are the same.

For example, for 2014, Jan, it would be 309 + 123 = 432. 432图 1

The final result should be something in the second image.图 2

results that should be on r is per following

前

后

You have not shared data for demonstratin and checking of code. However this should work.

df %>% group_by(Year, Month, category = ifelse(category %in% c("Category C", "Category C-ETS"), "Category C", category)) %>% summarise(number = sum(number))

One way to approach this, is by renaming cells with "Category C-Ets" as "Category C" and then using grroup_by and summarize from Dplyr this code solves this

library(dplyr)

df[df$category == "Category C-Ets", "category"] <-"Category C"

df <- df %>%
  group_by(year, month, category) %>%
  summarize(number = sum(number))

Check out below the step by step how to solve this

This is how I can approach this task, assuming df is your dataframe

#sample dataframe
df <- data.frame(year=c(2014,2014,2014,2014,2014,2014,2014,2014,2014,2014),
                 month = c(1,1,1,1,1,2,2,2,2,2),
                 category = c("Category C", "Category A", "Category C-Ets","CategoryB", "Category D", "Category C", "Category A", "Category C-Ets","CategoryB", "Category D"),
                 number = seq(100,1000,100))
print(df)

#this is how the row dataframe looks
 year month       category number
1  2014     1     Category C    100
2  2014     1     Category A    200
3  2014     1 Category C-Ets    300
4  2014     1      CategoryB    400
5  2014     1     Category D    500
6  2014     2     Category C    600
7  2014     2     Category A    700
8  2014     2 Category C-Ets    800
9  2014     2      CategoryB    900
10 2014     2     Category D   1000

then substitute "Category C" for "Category C-Ets"

df[df$category == "Category C-Ets", "category"] <-"Category C"

print(df) 
year month   category number
1  2014     1 Category C    100
2  2014     1 Category A    200
3  2014     1 Category C    300
4  2014     1  CategoryB    400
5  2014     1 Category D    500
6  2014     2 Category C    600
7  2014     2 Category A    700
8  2014     2 Category C    800
9  2014     2  CategoryB    900
10 2014     2 Category D   1000

now you can group them by year month and category and sum the numbers

df <- df %>%
  group_by(year, month, category) %>%
  summarize(number = sum(number))

print(df)

   year month category   number
  <dbl> <dbl> <chr>       <dbl>
1  2014     1 Category A    200
2  2014     1 Category C    400
3  2014     1 Category D    500
4  2014     1 CategoryB     400
5  2014     2 Category A    700
6  2014     2 Category C   1400
7  2014     2 Category D   1000
8  2014     2 CategoryB     900

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM