English 中文(简体)
有条件区分价值观
原标题:Dividing values based on a condition

我有这样的数据框架:

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

我想根据第二栏重新界定第三栏的数值。 我指的是,如果我在第二栏中拥有L&R希望将其价值区分为2(从第三栏中,在第五栏中等于5),并将结果添加到同一P1组的L和R。 因此,L&R=5/2将为2.5。 在P1组的L组中,这一2.5应加上22.5,P1组的R组应为77.5。 但是,如果我有两处,再有两处;在第2栏中,我想要把价值分为三个。 最后产出应如此:

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

我已经制定了一部法典,当时使用的是:&如下,但我未能在有<代码>和&时发挥作用。

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()

感谢任何帮助。

问题回答

您可按<代码>和>、+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




相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...

热门标签