回答編集履歴

3

2023/10/29 15:51

投稿

melian
melian

スコア20574

test CHANGED
@@ -1,13 +1,13 @@
1
- ```r
1
+ ```R
2
2
  suppressMessages(library(tidyverse))
3
3
 
4
- name <- c("Suzuki Yuta", "Suzuki Keiko", "Suzuki Koko", "Fujita Takashi", "Fujita Miki", "Fujita Tomoka ", "Ooshima Kiyo", "Ooshima Ken")
4
+ name <- c("Suzuki Yuta", "Suzuki Keiko", "Suzuki Koko", "Fujita Takashi", "Fujita Miki", "Fujita Tomoka", "Ooshima Kiyo", "Ooshima Ken")
5
5
  Income_January <- c(43, 74, 80, 37, 20, 83, 64, 35)
6
6
  Income_February <- c(55, 70, 82, 40, 22, 23, 60, 30)
7
7
  df1 <- tibble(name, Income_January, Income_February)
8
8
 
9
9
  df2 <- df1 %>%
10
- group_by(first_name = sub('^(.+?) .+$', '\\1', name)) %>%
10
+ group_by(last_name = sub('^(.+?) .+$', '\\1', name)) %>%
11
11
  summarize(
12
12
  Total_Income_January = sum(Income_January),
13
13
  Total_Income_February = sum(Income_February))
@@ -15,9 +15,10 @@
15
15
  df2
16
16
 
17
17
  # A tibble: 3 × 3
18
- # first_name Total_Income_January Total_Income_February
18
+ # last_name Total_Income_January Total_Income_February
19
- # <chr> <dbl> <dbl>
19
+ # <chr> <dbl> <dbl>
20
- # 1 Fujita 140 85
20
+ # 1 Fujita 140 85
21
- # 2 Ooshima 99 90
21
+ # 2 Ooshima 99 90
22
- # 3 Suzuki 197 207
22
+ # 3 Suzuki 197 207
23
23
  ```
24
+

2

2023/10/29 14:42

投稿

melian
melian

スコア20574

test CHANGED
@@ -6,11 +6,13 @@
6
6
  Income_February <- c(55, 70, 82, 40, 22, 23, 60, 30)
7
7
  df1 <- tibble(name, Income_January, Income_February)
8
8
 
9
- df1 %>%
9
+ df2 <- df1 %>%
10
- group_by(first_name = sub('^([^ ]+).+$', '\\1', name)) %>%
10
+ group_by(first_name = sub('^(.+?) .+$', '\\1', name)) %>%
11
11
  summarize(
12
12
  Total_Income_January = sum(Income_January),
13
13
  Total_Income_February = sum(Income_February))
14
+
15
+ df2
14
16
 
15
17
  # A tibble: 3 × 3
16
18
  # first_name Total_Income_January Total_Income_February

1

2023/10/29 14:34

投稿

melian
melian

スコア20574

test CHANGED
@@ -2,18 +2,18 @@
2
2
  suppressMessages(library(tidyverse))
3
3
 
4
4
  name <- c("Suzuki Yuta", "Suzuki Keiko", "Suzuki Koko", "Fujita Takashi", "Fujita Miki", "Fujita Tomoka ", "Ooshima Kiyo", "Ooshima Ken")
5
- Income_Janualy <- c(43, 74, 80, 37, 20, 83, 64, 35)
5
+ Income_January <- c(43, 74, 80, 37, 20, 83, 64, 35)
6
- Income_Februaly <- c(55, 70, 82, 40, 22, 23, 60, 30)
6
+ Income_February <- c(55, 70, 82, 40, 22, 23, 60, 30)
7
- df1 <- tibble(name, Income_Janualy, Income_Februaly)
7
+ df1 <- tibble(name, Income_January, Income_February)
8
8
 
9
9
  df1 %>%
10
10
  group_by(first_name = sub('^([^ ]+).+$', '\\1', name)) %>%
11
11
  summarize(
12
- Total_Income_January = sum(Income_Janualy),
12
+ Total_Income_January = sum(Income_January),
13
- Total_Income_Februaly = sum(Income_Februaly))
13
+ Total_Income_February = sum(Income_February))
14
14
 
15
15
  # A tibble: 3 × 3
16
- # first_name Total_Income_January Total_Income_Februaly
16
+ # first_name Total_Income_January Total_Income_February
17
17
  # <chr> <dbl> <dbl>
18
18
  # 1 Fujita 140 85
19
19
  # 2 Ooshima 99 90