Home > Software design >  How do I replace values in a category by group in R
How do I replace values in a category by group in R

Time:04-29

Hi I have a dataframe sleep_data where I am attempting to change Id values to user1:user33 based on groups. So where Id == 1503960366 change to user_1, Id == 1644430081 change to user_2, etc etc.

I am originally using this code, however would enjoy a more concise and clear code to update each user string as there are 33 different values and am looking for the most efficient way of achieving this.

sleep_data["Id"][sleep_data["Id"] == 1503960366 ] <- user_1
sleep_data["Id"][sleep_data["Id"] == 1644430081] <- user_2

structure(list(Id = c(1503960366, 1503960366, 1503960366, 1503960366, 1503960366, 1503960366, 1503960366, 1503960366, 1503960366, 1503960366, 1503960366, 1503960366, 1503960366, 1503960366, 1503960366, 1503960366, 1503960366, 1503960366, 1503960366, 1503960366, 1503960366, 1503960366, 1503960366, 1503960366, 1503960366, 1644430081, 1644430081, 1644430081, 1644430081, 1844505072, 1844505072, 1844505072, 1927972279, 1927972279, 1927972279, 1927972279, 1927972279, 2026352035, 2026352035, 2026352035, 2026352035, 2026352035, 2026352035, 2026352035, 2026352035, 2026352035, 2026352035, 2026352035, 2026352035, 2026352035), SleepDay = c("4/12/2016 12:00:00 AM", "4/13/2016 12:00:00 AM", "4/15/2016 12:00:00 AM", "4/16/2016 12:00:00 AM", "4/17/2016 12:00:00 AM", "4/19/2016 12:00:00 AM", "4/20/2016 12:00:00 AM", "4/21/2016 12:00:00 AM", "4/23/2016 12:00:00 AM", "4/24/2016 12:00:00 AM", "4/25/2016 12:00:00 AM", "4/26/2016 12:00:00 AM", "4/28/2016 12:00:00 AM", "4/29/2016 12:00:00 AM", "4/30/2016 12:00:00 AM", "5/1/2016 12:00:00 AM", "5/2/2016 12:00:00 AM", "5/3/2016 12:00:00 AM", "5/5/2016 12:00:00 AM", "5/6/2016 12:00:00 AM", "5/7/2016 12:00:00 AM", "5/8/2016 12:00:00 AM", "5/9/2016 12:00:00 AM", "5/10/2016 12:00:00 AM", "5/11/2016 12:00:00 AM", "4/29/2016 12:00:00 AM", "4/30/2016 12:00:00 AM", "5/2/2016 12:00:00 AM", "5/8/2016 12:00:00 AM", "4/15/2016 12:00:00 AM", "4/30/2016 12:00:00 AM", "5/1/2016 12:00:00 AM", "4/12/2016 12:00:00 AM", "4/13/2016 12:00:00 AM", "4/15/2016 12:00:00 AM", "4/26/2016 12:00:00 AM", "4/28/2016 12:00:00 AM", "4/12/2016 12:00:00 AM", "4/13/2016 12:00:00 AM", "4/14/2016 12:00:00 AM", "4/15/2016 12:00:00 AM", "4/16/2016 12:00:00 AM", "4/17/2016 12:00:00 AM", "4/19/2016 12:00:00 AM", "4/20/2016 12:00:00 AM", "4/21/2016 12:00:00 AM", "4/22/2016 12:00:00 AM", "4/23/2016 12:00:00 AM", "4/24/2016 12:00:00 AM", "4/25/2016 12:00:00 AM"), TotalSleepRecords = c(1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), TotalMinutesAsleep = c(327L, 384L, 412L, 340L, 700L, 304L, 360L, 325L, 361L, 430L, 277L, 245L, 366L, 341L, 404L, 369L, 277L, 273L, 247L, 334L, 331L, 594L, 338L, 383L, 285L, 119L, 124L, 796L, 137L, 644L, 722L, 590L, 750L, 398L, 475L, 296L, 166L, 503L, 531L, 545L, 523L, 524L, 437L, 498L, 461L, 477L, 520L, 522L, 555L, 506L), TotalTimeInBed = c(346L, 407L, 442L, 367L, 712L, 320L, 377L, 364L, 384L, 449L, 323L, 274L, 393L, 354L, 425L, 396L, 309L, 296L, 264L, 367L, 349L, 611L, 342L, 403L, 306L, 127L, 142L, 961L, 154L, 961L, 961L, 961L, 775L, 422L, 499L, 315L, 178L, 546L, 565L, 568L, 573L, 567L, 498L, 540L, 510L, 514L, 545L, 554L, 591L, 531L)), row.names = c(NA, 50L), class = "data.frame")

Thank you

CodePudding user response:

This should do the job, unless you want specific Ids to have specific User_x.

library(tidyverse)

Data %>% 
  dplyr::mutate(User = str_c("user_",
                             Data %>%
                               dplyr::group_by(Id) %>% 
                               dplyr::group_indices())) %>% 
  dplyr::relocate(User,.after = Id)

Output:

           Id   User              SleepDay TotalSleepRecords TotalMinutesAsleep TotalTimeInBed
1  1503960366 user_1 4/12/2016 12:00:00 AM                 1                327            346
2  1503960366 user_1 4/13/2016 12:00:00 AM                 2                384            407
3  1503960366 user_1 4/15/2016 12:00:00 AM                 1                412            442
4  1503960366 user_1 4/16/2016 12:00:00 AM                 2                340            367
5  1503960366 user_1 4/17/2016 12:00:00 AM                 1                700            712
6  1503960366 user_1 4/19/2016 12:00:00 AM                 1                304            320
7  1503960366 user_1 4/20/2016 12:00:00 AM                 1                360            377
8  1503960366 user_1 4/21/2016 12:00:00 AM                 1                325            364
9  1503960366 user_1 4/23/2016 12:00:00 AM                 1                361            384
10 1503960366 user_1 4/24/2016 12:00:00 AM                 1                430            449
11 1503960366 user_1 4/25/2016 12:00:00 AM                 1                277            323
12 1503960366 user_1 4/26/2016 12:00:00 AM                 1                245            274
13 1503960366 user_1 4/28/2016 12:00:00 AM                 1                366            393
14 1503960366 user_1 4/29/2016 12:00:00 AM                 1                341            354
15 1503960366 user_1 4/30/2016 12:00:00 AM                 1                404            425
16 1503960366 user_1  5/1/2016 12:00:00 AM                 1                369            396
17 1503960366 user_1  5/2/2016 12:00:00 AM                 1                277            309
18 1503960366 user_1  5/3/2016 12:00:00 AM                 1                273            296
19 1503960366 user_1  5/5/2016 12:00:00 AM                 1                247            264
20 1503960366 user_1  5/6/2016 12:00:00 AM                 1                334            367
21 1503960366 user_1  5/7/2016 12:00:00 AM                 1                331            349
22 1503960366 user_1  5/8/2016 12:00:00 AM                 1                594            611
23 1503960366 user_1  5/9/2016 12:00:00 AM                 1                338            342
24 1503960366 user_1 5/10/2016 12:00:00 AM                 1                383            403
25 1503960366 user_1 5/11/2016 12:00:00 AM                 1                285            306
26 1644430081 user_2 4/29/2016 12:00:00 AM                 1                119            127
27 1644430081 user_2 4/30/2016 12:00:00 AM                 1                124            142
28 1644430081 user_2  5/2/2016 12:00:00 AM                 1                796            961
29 1644430081 user_2  5/8/2016 12:00:00 AM                 1                137            154
30 1844505072 user_3 4/15/2016 12:00:00 AM                 1                644            961
31 1844505072 user_3 4/30/2016 12:00:00 AM                 1                722            961
32 1844505072 user_3  5/1/2016 12:00:00 AM                 1                590            961
33 1927972279 user_4 4/12/2016 12:00:00 AM                 3                750            775
34 1927972279 user_4 4/13/2016 12:00:00 AM                 1                398            422
35 1927972279 user_4 4/15/2016 12:00:00 AM                 2                475            499
36 1927972279 user_4 4/26/2016 12:00:00 AM                 1                296            315
37 1927972279 user_4 4/28/2016 12:00:00 AM                 1                166            178
38 2026352035 user_5 4/12/2016 12:00:00 AM                 1                503            546
39 2026352035 user_5 4/13/2016 12:00:00 AM                 1                531            565
40 2026352035 user_5 4/14/2016 12:00:00 AM                 1                545            568
41 2026352035 user_5 4/15/2016 12:00:00 AM                 1                523            573
42 2026352035 user_5 4/16/2016 12:00:00 AM                 1                524            567
43 2026352035 user_5 4/17/2016 12:00:00 AM                 1                437            498
44 2026352035 user_5 4/19/2016 12:00:00 AM                 1                498            540
45 2026352035 user_5 4/20/2016 12:00:00 AM                 1                461            510
46 2026352035 user_5 4/21/2016 12:00:00 AM                 1                477            514
47 2026352035 user_5 4/22/2016 12:00:00 AM                 1                520            545
48 2026352035 user_5 4/23/2016 12:00:00 AM                 1                522            554
49 2026352035 user_5 4/24/2016 12:00:00 AM                 1                555            591
50 2026352035 user_5 4/25/2016 12:00:00 AM                 1                506            531

CodePudding user response:

Using data.table:

library(data.table)
Users <- c('user_1', 'user_3', 'user_4', 'user_5', 'user_7') 
Id    <- c(1503960366, 1644430081, 1844505072, 1927972279, 2026352035)
users <- data.table(Users, Id)
setDT(sleepdata)[users, User:=i.Users, on=.(Id)]
##
#   validate
#
sleepdata[, .N, by=.(Id, User)]
##            Id   User  N
## 1: 1503960366 user_1 25
## 2: 1644430081 user_3  4
## 3: 1844505072 user_4  3
## 4: 1927972279 user_5  5
## 5: 2026352035 user_7 13
  • Related