我有一张宽大的桌子,上面有几列,下面我将进行简要介绍。该表是通过MicrosoftAccess引入的,因此“索引”列无处不在,尤其没有任何关联。基本上,我想向表中添加另一列,以按最旧日期->最新日期对行进行索引。因此,无论其他条件如何,我都希望最早的日期具有“ 1”,并按时间顺序2,3,4,5等到最后一个日期。
index- effort_ID- Tag ID- SUR- Date and Time
350162 - 244 - 92 - 10916 - 2016-12-14 19:25:00
77850 - 243 -77- 10913 -2016-12-14 19:28:10
77858 - 243- 79 -10913 -2016-12-14 19:39:11
答案 0 :(得分:0)
以下是格式更好的数据:
df <- structure(list(index = c(350162, 77850, 77858), effort_ID = c(244,
243, 243), `Tag ID` = c(92, 77, 79), SUR = c(10916, 10913, 10913
), `Date and Time` = c("2016-12-14 19:25:00", "2016-12-14 19:28:10",
"2016-12-14 19:39:11")), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -3L))
然后使用lubridate
设置日期时间格式arrange
并将行索引设置为tidyverse
中的列
library(lubridate)
library(tidyverse)
df2 <- df %>%
mutate(`Date and Time` = ymd_hms(`Date and Time`)) %>%
arrange(`Date and Time`) %>%
rownames_to_column(var = "new_index")
结果:
# A tibble: 3 x 6
new_index index effort_ID `Tag ID` SUR `Date and Time`
<chr> <dbl> <dbl> <dbl> <dbl> <dttm>
1 1 350162 244 92 10916 2016-12-14 19:25:00
2 2 77850 243 77 10913 2016-12-14 19:28:10
3 3 77858 243 79 10913 2016-12-14 19:39:11
答案 1 :(得分:0)
首先使用dplyr软件包安排df,然后使用mutate将列添加到索引
使用管道运算符(%>%
)很容易
library(dplyr)
df %>% arrange(`Date and Time`) %>%
mutate(new_index = 1:nrow(df))
答案 2 :(得分:0)
MyLib.js
解决方案
class MyLib {
constructor() {
// do stuff
}
// other functions that do stuff
processArray() {
}
processString() {
}
}
export default MyLib;
答案 3 :(得分:0)
还有一种可能的解决方案,无需使用库
str<-c( "77858 - 243- 79 -10913 -2016-12-14 19:39:11",
"350162 - 244 - 92 - 10916 - 2016-12-14 19:25:00",
"77850 - 243 -77- 10913 -2016-12-14 19:28:10")
customer<-c("lina","rita","mina")
df <- data.frame(cust=customer,date=str)
df
cust date
1 lina 77858 - 243- 79 -10913 -2016-12-14 19:39:11
2 rita 350162 - 244 - 92 - 10916 - 2016-12-14 19:25:00
3 mina 77850 - 243 -77- 10913 -2016-12-14 19:28:10
之后
str<-as.character(substr(str,(nchar(str)+1)-19,nchar(str)))
str
"2016-12-14 19:39:11" "2016-12-14 19:25:00" "2016-12-14 19:28:10"
df$newDate=strptime(str, "%Y-%m-%d %H:%M:%S")
rownames(df) <- order(df$newDate)
df
cust date newDate
2 lina 77858 - 243- 79 -10913 -2016-12-14 19:39:11 2016-12-14 19:39:11
3 rita 350162 - 244 - 92 - 10916 - 2016-12-14 19:25:00 2016-12-14 19:25:00
1 mina 77850 - 243 -77- 10913 -2016-12-14 19:28:10 2016-12-14 19:28:10
最后
df[order(as.numeric(rownames(df))),,drop=FALSE]
df
cust date newDate
1 mina 77850 - 243 -77- 10913 -2016-12-14 19:28:10 2016-12-14 19:28:10
2 lina 77858 - 243- 79 -10913 -2016-12-14 19:39:11 2016-12-14 19:39:11
3 rita 350162 - 244 - 92 - 10916 - 2016-12-14 19:25:00 2016-12-14 19:25:00