Home > Blockchain >  How to change row names within a Variable in R
How to change row names within a Variable in R

Time:04-06

how do I change row names within this variable in my data frame? I would like to change all ActivityHour 00AM 1AM 2AM 8AM 9AM 10AM 11AM 12PM 1PM 2PM 3PM

structure(list(Id = c("user_1", "user_1", "user_1", "user_1", 
"user_1", "user_1", "user_1", "user_1", "user_1", "user_1"), 
ActivityHour = c("0:00", "1:00", "2:00", "8:00", "9:00", 
"10:00", "11:00", "12:00", "13:00", "14:00"), TotalIntensity = c(20, 
8, 7, 13, 30, 29, 12, 11, 6, 36), AverageIntensity = c(0.333333, 
0.133333, 0.116667, 0.216667, 0.5, 0.483333, 0.2, 0.183333, 
0.1, 0.6)), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame"))

Thank you!

CodePudding user response:

You are changing the variable ActivityHour. You are not changing row names which for your example are c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10"). This will change the ActivityHour variable assuming the data structure you provided is called example:

hours <- paste0(0:23, ":00")
ampm <- paste0(c(0:12, 1:11), c(rep("AM", 13), rep("PM", 11)))
timetable <- data.frame(hours, ampm)
example$ActivityHour
#  [1] "0:00"  "1:00"  "2:00"  "8:00"  "9:00"  "10:00" "11:00" "12:00" "13:00" "14:00"
example$ActivityHour <- timetable$ampm[match(example$ActivityHour, timetable$hours)]
example$ActivityHour
#  [1] "0AM"  "1AM"  "2AM"  "8AM"  "9AM"  "10AM" "11AM" "12AM" "1PM"  "2PM" 

CodePudding user response:

Is this what you're trying to do?

df <- structure(list(Id = c("user_1", "user_1", "user_1", "user_1", 
                            "user_1", "user_1", "user_1", "user_1", "user_1", "user_1"), 
                     ActivityHour = c("0:00", "1:00", "2:00", "8:00", "9:00", 
                                      "10:00", "11:00", "12:00", "13:00", "14:00"),
                     TotalIntensity = c(20, 8, 7, 13, 30, 29, 12, 11, 6, 36), 
                     AverageIntensity = c(0.333333, 0.133333, 0.116667, 0.216667,
                                          0.5, 0.483333, 0.2, 0.183333, 0.1, 0.6)), 
                row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"))

df$ActivityHour <- toupper(format(strptime(df$ActivityHour, format = "%H:%M"), '%I%p'))
df
#> # A tibble: 10 × 4
#>    Id     ActivityHour TotalIntensity AverageIntensity
#>    <chr>  <chr>                 <dbl>            <dbl>
#>  1 user_1 12AM                     20            0.333
#>  2 user_1 01AM                      8            0.133
#>  3 user_1 02AM                      7            0.117
#>  4 user_1 08AM                     13            0.217
#>  5 user_1 09AM                     30            0.5  
#>  6 user_1 10AM                     29            0.483
#>  7 user_1 11AM                     12            0.2  
#>  8 user_1 12PM                     11            0.183
#>  9 user_1 01PM                      6            0.1  
#> 10 user_1 02PM                     36            0.6

Created on 2022-04-06 by the reprex package (v2.0.1)

  • Related