根据条件分割数值

huangapple go评论68阅读模式
英文:

Dividing values based on a condition

问题

Here's a translation of the code part you provided:

library(tidyverse)

df %>%
  filter(!str_count(Col2, "&") > 1) %>%
  mutate(Value = ifelse(grepl("&", Col2), as.numeric(Value) / 2, as.numeric(Value))) %>%
  separate_rows(Col2, sep = "&") %>%
  group_by(Col1, Col2) %>%
  summarise(Value = sum(Value)) %>%
  ungroup()

This code performs the desired transformation on your DataFrame based on the conditions you described. If you need further assistance or have questions about the code, please feel free to ask.

英文:

I have data frame like this one:

df<- data.frame(
  "Col1" = c("P1", "P1", "P1", "P2", "P2", "P2", "P3", "P3", "P3",
              "P3"),
  "Col2" = c("L", "L&R", "R", "V", "V&N", "N", "M", "I", "I&M",
             "I&M&G"),
  "Value" = c("20", "5", "75", "30", "7", "63", "10", "80", "2","8"))
df

I want to redefine the values in the third column based on the second column. I mean when I have L&R in the second column I want to divide its value by 2 (from the third column which equals to 5 in this case 5) and add this results to both L and R with in the same P1 group. So, L&R=5/2 will be 2.5. This 2.5 should be added to L in P1 group to be 22.5 and to R in P1 group to be 77.5. But in a case if I have two && in the column 2 ,I want the value to be divided by 3. The final output should look like this:

df.output<- data.frame(
  "Col1" = c("P1",  "P1", "P2",  "P2", "P3", "P3","P3"),
  "Col2" = c("L",  "R", "V",  "N", "M", "I","G" ),
  "Value" = c("22.5",  "77.5", "33.5",  "66.5", "13.66",  "83.66","2.66"))
df.output
df.output
  Col1 Col2 Value
1   P1    L  22.5
2   P1    R  77.5
3   P2    V  33.5
4   P2    N  66.5
5   P3    M 13.66
6   P3    I 83.66
7   P3    G  2.66

I already had a code that worked when with one & as below but I failed to make it work when we have &&.

library(tidyverse)

df %>%
  filter(!str_count(Col2, "&") > 1) %>%
  mutate(Value = ifelse(grepl("&", Col2), as.numeric(Value) / 2, as.numeric(Value))) %>%
  separate_rows(Col2, sep = "&") %>%
  group_by(Col1, Col2) %>%
  summarise(Value = sum(Value)) %>%
  ungroup()

Any help is appreciated.

答案1

得分: 2

你可以将Value除以&的数量加1。

library(tidyverse)

df %>%
  mutate(Value = as.numeric(Value)/(str_count(Col2, "&") + 1)) %>%
  separate_longer_delim(Col2, delim = "&") %>%
  summarize(Value = sum(Value), .by = c(Col1, Col2))

  Col1 Col2     Value
1   P1    L 22.500000
2   P1    R 77.500000
3   P2    V 33.500000
4   P2    N 66.500000
5   P3    M 13.666667
6   P3    I 83.666667
7   P3    G  2.666667
英文:

You can divide Value by the number of & + 1.

library(tidyverse)

df %>% 
  mutate(Value = as.numeric(Value)/(str_count(Col2, "&") + 1)) %>% 
  separate_longer_delim(Col2, delim = "&") %>% 
  summarize(Value = sum(Value), .by = c(Col1, Col2))

  Col1 Col2     Value
1   P1    L 22.500000
2   P1    R 77.500000
3   P2    V 33.500000
4   P2    N 66.500000
5   P3    M 13.666667
6   P3    I 83.666667
7   P3    G  2.666667

huangapple
  • 本文由 发表于 2023年5月26日 10:46:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76337348.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定