-1

I'm learning new data analysis skills using R and need help with cleaning. I have 582 rows where the started_at and ended_at column values are inverted (start times inputed in end_at column and vis a verse). Is there a code that can move these values to their proper columns?

enter image description here

  • Note the instructions at the top of the [tag:r] tag page and in particular where it says not to post images of input data. Show it using the output from `dput` as no one wants to reytpe it all. Also show the expected result. – G. Grothendieck Jan 08 '22 at 17:35
  • 1
    How about changing the column names? E.g. `colnames(data)[1:2] <- colnames(data)[2:1]` – Andre Wildberg Jan 08 '22 at 17:42
  • Swapping columns? `tmp <- dat[[1]]; dat[[1]] <- dat[[2]]; dat[[2]] <- tmp;`. If this needs to be done conditionally, we need more (usable) information. Perhaps this is a dupe of https://stackoverflow.com/q/26448893/3358272. – r2evans Jan 08 '22 at 17:54
  • I suspect what you want is that if duration < 0 then switch the start and the end values? –  Jan 08 '22 at 17:57

1 Answers1

2

We could use select

library(dplyr)
select(df, started_at=ended_at, ended_at=started_at, everything())
            started_at            ended_at member_casual duration
1  2021-09-04 17:20:35 2021-09-04 17:20:37        casual    -0.03
2  2021-03-29 15:41:20 2021-03-29 15:41:21        member    -0.02
3  2021-03-13 18:02:57 2021-03-13 18:02:58        casual    -0.02
4  2021-05-20 12:31:52 2021-05-20 12:31:53        member    -0.02
5  2021-06-20 10:52:25 2021-06-20 10:52:26        casual    -0.02
6  2021-06-28 13:18:25 2021-06-28 13:18:26        member    -0.02
7  2021-06-28 14:56:27 2021-06-28 14:56:28        casual    -0.02
8  2021-07-27 19:11:54 2021-07-27 19:11:55        member    -0.02
9  2021-07-25 01:38:22 2021-07-25 01:38:23        casual    -0.02
10 2021-08-22 20:15:46 2021-08-22 20:15:47        member    -0.02
11 2021-09-29 14:02:51 2021-09-29 14:02:52        member    -0.02
12 2021-09-26 11:15:16 2021-09-26 11:15:17        casual    -0.02
13 2021-09-29 15:26:22 2021-09-29 15:26:23        member    -0.02

data:

structure(list(started_at = c("2021-09-04 17:20:37", "2021-03-29 15:41:21", 
"2021-03-13 18:02:58", "2021-05-20 12:31:53", "2021-06-20 10:52:26", 
"2021-06-28 13:18:26", "2021-06-28 14:56:28", "2021-07-27 19:11:55", 
"2021-07-25 01:38:23", "2021-08-22 20:15:47", "2021-09-29 14:02:52", 
"2021-09-26 11:15:17", "2021-09-29 15:26:23"), ended_at = c("2021-09-04 17:20:35", 
"2021-03-29 15:41:20", "2021-03-13 18:02:57", "2021-05-20 12:31:52", 
"2021-06-20 10:52:25", "2021-06-28 13:18:25", "2021-06-28 14:56:27", 
"2021-07-27 19:11:54", "2021-07-25 01:38:22", "2021-08-22 20:15:46", 
"2021-09-29 14:02:51", "2021-09-26 11:15:16", "2021-09-29 15:26:22"
), member_casual = c("casual", "member", "casual", "member", 
"casual", "member", "casual", "member", "casual", "member", "member", 
"casual", "member"), duration = c(-0.03, -0.02, -0.02, -0.02, 
-0.02, -0.02, -0.02, -0.02, -0.02, -0.02, -0.02, -0.02, -0.02
)), class = "data.frame", row.names = c(NA, -13L))
TarJae
  • 72,363
  • 6
  • 19
  • 66