我有一个这样的数据框。
ID <- c("111","111","111","111",
"113","113","113","113")
ToolID <- c("CCP_A","CCP_B","CCP_B","CCQ_A",
"CCP_A","CCP_B","CCP_B","CCQ_A")
Step <- c("Step_A","Step_B","Step_B","Step_C",
"Step_A","Step_B","Step_B","Step_C")
Datetime <- c("2018-04-19 12:44:11","2018-04-17 12:44:11","2018-04-17 12:44:11","2018-04-16 21:11:09",
"2018-04-13 12:44:11","2018-04-11 12:44:11","2018-04-11 12:44:11","2018-04-10 21:11:09")
df <- data.frame(ID,ToolID,Step,Datetime)
df$Datetime <- as.POSIXct(df$Datetime,format="%Y-%m-%d %H:%M:%S")
我正在尝试创建一个新列“ New.Datetime”,它是仅在ToolID == "CCP_B" & Step=="Step_B"
这是我想要的输出
ID ToolID Step Datetime New.Datetime
111 CCP_A Step_A 2018-04-19 12:44:11 2018-04-17 12:44:11
111 CCP_B Step_B 2018-04-17 12:44:11 2018-04-17 12:44:11
111 CCP_B Step_B 2018-04-17 12:44:11 2018-04-17 12:44:11
111 CCQ_A Step_C 2018-04-16 21:11:09 2018-04-17 12:44:11
113 CCP_A Step_A 2018-04-13 12:44:11 2018-04-11 12:44:11
113 CCP_B Step_B 2018-04-11 12:44:11 2018-04-11 12:44:11
113 CCP_B Step_B 2018-04-11 12:44:11 2018-04-11 12:44:11
113 CCQ_A Step_C 2018-04-10 21:11:09 2018-04-11 12:44:11
我正在尝试这样做,但是new.datetime似乎与数字值杂乱无章。
library(dplyr)
df1 <- df %>%
group_by(ID) %>%
mutate(New.Datetime = ifelse((ToolID == "CCP_B" & Step=="Step_B"),
max(Datetime), Datetime))
答案 0 :(得分:3)
我们可以使用// my.component.ts
import * as fileSaver from 'file-saver';
export class MyComponent {
constructor(private http: HttpClient){}
downloadFile(path: string) {
this.startLoading();
this.http.get(`${MY_APP_URL}/${path}`, { responseType: 'blob' })
.pipe(tap(blob: Blob => fileSaver.saveAs(blob, 'your_csv_file_name.csv')))
.subscribe(() => this.stopLoading(), err => this.handleErr(err));
}
}
case_when
正如评论中的OP所述,根据ecah'ID'的条件,其值为df %>%
group_by(ID) %>%
mutate(New.DateTime = case_when(ToolID == "CCP_B" &
Step == "Step_B" ~ max(Datetime),
TRUE ~ Datetime))
# A tibble: 8 x 5
# Groups: ID [2]
# ID ToolID Step Datetime New.DateTime
# <fct> <fct> <fct> <dttm> <dttm>
#1 111 CCP_A Step_A 2018-04-19 12:44:11 2018-04-19 12:44:11
#2 111 CCP_B Step_B 2018-04-17 12:44:11 2018-04-19 12:44:11
#3 111 CCP_B Step_B 2018-04-17 12:44:11 2018-04-19 12:44:11
#4 111 CCQ_A Step_C 2018-04-16 21:11:09 2018-04-16 21:11:09
#5 113 CCP_A Step_A 2018-04-13 12:44:11 2018-04-13 12:44:11
#6 113 CCP_B Step_B 2018-04-11 12:44:11 2018-04-13 12:44:11
#7 113 CCP_B Step_B 2018-04-11 12:44:11 2018-04-13 12:44:11
#8 113 CCQ_A Step_C 2018-04-10 21:11:09 2018-04-10 21:11:09
,我们只需要
max