根据列中的正则表达式语法,将变量添加到包含每一行最大值的数据框中

问题描述

我有一个非常庞大的数据集,其中包含结果和日期。数据的一小部分(我还有更多具有不同名称和行的列):

  result_1    date_1 result_2    date_2 result_3    date_3 result_4    date_4
1        1 12.8.2020        4 13.8.2020        2 15.8.2020        1 20.8.2020
2        3 15.8.2020        3 14.8.2020        5 17.8.2020        2 21.8.2020

我想为结果列添加一个最大列和一个最大名称列,具体取决于列名称(我使用regex,因为并非所有列都正确命名) 我尝试了一些选项,一种似乎可行的方法是在选择包含我选择的正则表达式的列时创建另一个数据框。 我尝试了类似于以下代码的内容:

data_max <- data %>% 
  select(matches("result_")) %>% 
  rowwise() %>% 
  mutate(max = max(.))

我的想法是然后将max列与原始数据连接起来并找到列名,但是我敢肯定有一种更好的方法,尤其是因为我的数据包含其他列名(不同的正则表达式)我还想按行最大化,并在某些列中最大化na。

解决方法

您可以选择 //Button I want to create the annotation with in `CreateDetailsViewControllwer` : @IBAction func publishButtonPressed(_ sender: UIButton) { performSegue(withIdentifier: "goToMap",sender: self) func prepare (for segue: UIStoryboardSegue,sender: Any?) { let MapVC = segue.destination as! MapViewController var london = MKPointAnnotation() london.title = "London" london.coordinate = CLLocationCoordinate2DMake(51.508530,-0.076132) MapVC.mapView.addAnnotation(london) func mapView(_ mapView: MKMapView,viewFor annotation: MKAnnotation) -> MKAnnotationView? { guard annotation is MKPointAnnotation else {return nil} let identifier = "Annotation" var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) if annotationView == nil { annotationView = MKPinAnnotationView(annotation: annotation,reuseIdentifier: identifier) annotationView!.canShowCallout = true } else { annotationView!.annotation = annotation } return annotationView } 列并使用'result'

max.col

这将为cols <- grep('result',names(df),value = TRUE) df$max_column <- cols[max.col(df[cols],ties.method = 'first')] df # result_1 date_1 result_2 date_2 result_3 date_3 result_4 date_4 max_column #1 1 12.8.2020 4 13.8.2020 2 15.8.2020 1 20.8.2020 result_2 #2 3 15.8.2020 3 14.8.2020 5 17.8.2020 2 21.8.2020 result_3 列的每一行提供最大值的列名。

数据

'result'

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...