我正在R中进行循环和函数训练(但目前处于非常基本的水平)。对于最近的研究,我需要准备以下数据:
我有一个看起来像这样的数据集:
dd <- read.table(text="
event.timeline.ys ID year group
1 2 800033 2008 A
2 1 800033 2009 A
3 0 800033 2010 A
4 -1 800033 2011 A
5 -2 800033 2012 A
15 0 800076 2008 B
16 -1 800076 2009 B
17 5 800100 2014 C
18 4 800100 2015 C
19 2 800100 2017 C
20 1 800100 2018 C
30 0 800125 2008 A
31 -1 800125 2009 A
32 -2 800125 2010 A", header=TRUE)
我只想为每个人保留event.timeline.ys> = 0的 last 行(ID 800033为第3行)和 first > event.timeline.ys <0的行(这将是ID 800033的第4行)。所有其他行将被删除。因此,我的最终数据帧应每个ID仅包含两行。
ID = 800100的人的event.timeline.ys上没有任何负值。在这种情况下,我只想保留event.timeline.ys> = 0的最后一行。
然后,最终数据集将如下所示:
event.timeline.ys ID year group
3 0 800033 2010 A
4 -1 800033 2011 A
15 0 800076 2008 B
16 -1 800076 2009 B
20 1 800100 2018 C
30 0 800125 2008 A
31 -1 800125 2009 A
我考虑过使用for循环在每个ID中检查带event.timeline.ys> = 0的 last 行和带事件的 first 行。 timeline.ys <0是。但是,R中的实际实现失败。
有人建议吗?我也非常欢迎其他不基于for循环或类似内容的解决方案。
答案 0 :(得分:1)
以下是在dplyr中使用group_by
的一种选择:
dd %>% group_by(ID, category = event.timeline.ys >= 0) %>%
filter(abs(event.timeline.ys) == min(abs(event.timeline.ys))) %>%
dplyr::select(-category) %>%
as.data.frame
category event.timeline.ys ID year group
1 TRUE 0 800033 2010 A
2 FALSE -1 800033 2011 A
3 TRUE 0 800076 2008 B
4 FALSE -1 800076 2009 B
5 TRUE 1 800100 2018 C
6 TRUE 0 800125 2008 A
7 FALSE -1 800125 2009 A
答案 1 :(得分:0)
这是一种使用which()
和row_number()
提取您感兴趣的行的索引的方法
library(dplyr)
dd %>%
group_by(ID) %>%
filter(row_number() == last(which(event.timeline.ys >= 0)) |
row_number() == first(which(event.timeline.ys < 0)))
我认为阅读的好处类似于您用言语描述自己所追求的方式,希望如此有意义。
答案 2 :(得分:0)
按ID
分组,以及event.timesline.ys
是否为负。如果为负,请选择第一行(slice
),否则选择最后一行(n()
行)。
library(dplyr)
dd %>%
mutate(neg = event.timeline.ys < 0) %>%
group_by(ID, neg) %>%
slice(if(neg[1]) 1 else n()) %>%
ungroup %>%
select(-neg)
# # A tibble: 7 x 4
# event.timeline.ys ID year group
# <int> <int> <int> <fct>
# 1 0 800033 2010 A
# 2 -1 800033 2011 A
# 3 0 800076 2008 B
# 4 -1 800076 2009 B
# 5 1 800100 2018 C
# 6 0 800125 2008 A
# 7 -1 800125 2009 A
答案 3 :(得分:0)
这是在 @IBOutlet weak var btnTimeLabel : UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let when = DispatchTime.now() + 0.1 // change 2 to desired number of seconds
DispatchQueue.main.asyncAfter(deadline: when) {
Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(ViewController.countDownDate), userInfo: nil, repeats: true)
}
}
@objc func countDownDate() {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-mm-yyyy" //Your date format
dateFormatter.timeZone = TimeZone(abbreviation: "GMT+0:00") //Current time zone
let futuredate = dateFormatter.date(from: "29-4-2019") //according to date format your date string
var calendar = Calendar.current
let diffDateComponents = calendar.dateComponents([.day, .hour, .minute, .second], from: Date(), to: futuredate!)
print (Date())
print(diffDateComponents)
let countdown = "Days \(String(describing:diffDateComponents.day!)), Hours: \(String(describing: diffDateComponents.hour!)), Minutes: \(String(describing: diffDateComponents.minute!)), Seconds: \(String(describing: diffDateComponents.second!))"
print("countdown",countdown)
var dayText = String(describing: diffDateComponents.day!) + "d "
var hourText = String(describing: diffDateComponents.hour!) + "h "
btnTimeLabel.setTitle(dayText + hourText + String(describing: diffDateComponents.minute!) + "m " + String(describing: diffDateComponents.second!) + "s", for: .normal)
//print(dayText + hourText + String(describing: diffDateComponents.minute!) + "m " + String(describing: diffDateComponents.second!) + "s")
} // Dispose of any resources that can be recreated.
}
data.table