-1

I am currently trying to decrease the values in a column randomly according to a given sum. For example, if the main data is like this;

ID Value

1 4
2 10
3 16

after running the code the sum of Value should be 10 and this need to be done randomly(the decrease for each member should be chosen randomly)

ID Value

1 1
2 8
3 1

Tried several command and library but could not manage it. Still a novice and Any help would be appreciated!

Thanks

Edit: Sorry I was not clear enough. I would like to assign a new value for each observation smaller than original (randomly). And at the end new sum of value will be equal to 10

mel099
  • 23
  • 4

1 Answers1

1

Using the sample data

dd <- read.table(text="ID Value
1 4
2 10
3 16", header=TRUE)

and the dplyr + tidyr library, you can do

library(dplyr)
library(tidyr)

dd %>% 
  mutate(ID=factor(ID)) %>% 
  uncount(Value) %>%
  sample_n(10) %>% 
  count(ID, name = "Value", .drop=FALSE)

Here we repeat the row once for each Value, then we randomly sample 10 rows, then we count them back up. We turn ID to a factor to make sure IDs with 0 observations are preserved.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • but this doesn't guarantee that every `ID` would be included in the final output. OP hasn't mentioned any such constraint though. – Ronak Shah Sep 30 '21 at 04:19
  • True. Some IDs may not appear if you are only selecting 10, but if that's a constraint then it wouldn't be very "random" – MrFlick Sep 30 '21 at 04:20
  • What I think op wanted to convey, is that they want to assign a new value that is smaller than the original value (i.e. 'decrease the values') for each observation (i.e. keeping all rows) so that the new colSums is equal to 10... But it's kind of confusingly worded and not very clear. – dario Sep 30 '21 at 09:56
  • Hi, thanks for the answers, Yeah you are right I think I was not clear enough. What I want (like you mentioned) , assign a new value that is lower than original value for each observation (randomly) so that new sum of value will be equal to 10. I will edit my question. – mel099 Sep 30 '21 at 13:40
  • So is zero a valid value or.not? – MrFlick Sep 30 '21 at 15:02
  • Yes, some of the ID can be removed – mel099 Sep 30 '21 at 17:32
  • OK. I've updated the code to keep rows with 0 counts. – MrFlick Sep 30 '21 at 17:37
  • Thanks a lot , this works perfectly – mel099 Oct 01 '21 at 15:59