如何使用R在同一数据集中组合和求和两行

时间:2017-11-25 02:08:27

标签: r

我有一个由两列组成的数据集:“species_id”和“count”。有些物种是重复的,但命名不同,例如:BROC和broc。我想将这两行合并为一行,并将它们的计数值相加。

目前,我有:

species_id count
BRBL       109
BROC       16
broc       7
BRSP       16

我想要:

species_id count
BRBL       109
BROC       23
BRSP       16

非常感谢!任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

假设名称的差异仅为大写/小写,则可能会起作用:

library(dplyr)
df <- data_frame(species_id = c("BROC", "broc"), count = c(16, 7)) #sample data
df %>% mutate(species_id = toupper(species_id)) %>% 
    group_by(species_id) %>% summarise(count = sum(count))

如果除了大小写之外还有差异,那么在分组之前你可能需要使用正则表达式和其他数据清理技术,但这个想法应该是相同的。

答案 1 :(得分:0)

您可以使用

func showFooter(show: Bool) {
    DispatchQueue.main.safeAsync {
        if show {
            let loadingFooter = UIActivityIndicatorView(activityIndicatorStyle: .gray)
            UIApplication.shared.keyWindow?.addSubview(loadingFooter)
            loadingFooter.startAnimating()
            loadingFooter.snp.makeConstraints { make in
                make.centerX.equalTo(self.view.snp.centerX)
                make.height.equalTo(20)
                make.bottom.equalTo(self.view.snp.bottom).inset(49)
            }
        } else {
            UIApplication.shared.keyWindow?.subviews.forEach { ($0 as? UIActivityIndicatorView)?.removeFromSuperview() }
        }
    }
}

public struct ConstraintViewDSL: ConstraintAttributesDSL { 
    public func makeConstraints(_ closure: (_ make: ConstraintMaker) -> Void) {
        ConstraintMaker.makeConstraints(item: self.view, closure: closure)
    }
}

public extension ConstraintView {
    public var snp: ConstraintViewDSL {
        return ConstraintViewDSL(view: self)
    }
}

示例:

Fatal Exception: NSGenericException
0  CoreFoundation                 0x182609d04 __exceptionPreprocess
1  libobjc.A.dylib                0x181858528 objc_exception_throw
2  CoreFoundation                 0x182609c4c -[NSException initWithCoder:]
3  Foundation                     0x183118d18 -[NSLayoutConstraint _setActive:mutuallyExclusiveConstraints:]
4  Foundation                     0x183119004 __55+[NSLayoutConstraint _addOrRemoveConstraints:activate:]_block_invoke
5  Foundation                     0x182f3b59c -[NSISEngine withBehaviors:performModifications:]
6  Foundation                     0x183118ed4 +[NSLayoutConstraint _addOrRemoveConstraints:activate:]
7  SnapKit                        0x10281b54c specialized static ConstraintMaker.makeConstraints(item : LayoutConstraintItem, closure : (ConstraintMaker) -> ()) -> () (ConstraintMaker.swift:272)
8  SnapKit                        0x10281e578 ConstraintViewDSL.makeConstraints((ConstraintMaker) -> ()) -> () (ConstraintViewDSL.swift)
9  Zattini                        0x101341fc0 specialized UIViewController.(showFooter(show : Bool) -> ()).(closure #1) (UIViewController+Extensions.swift:140)
10 Zattini                        0x101340dd8 UIViewController.showFooter(show : Bool) -> () (UIViewController+Extensions.swift:14)
11 Zattini                        0x101340f10 @objc UIViewController.showFooter(show : Bool) -> () (UIViewController+Extensions.swift)
12 Zattini                        0x10132f5c4 ProductListControllerZattiniProductListDelegate (ProductListController.swift:197)
13 Zattini                        0x1013881cc ProductListViewModel.loadMoreProducts() -> () (ProductListViewModel.swift:159)
14 Zattini                        0x101331858 specialized ProductListController.scrollViewDidScroll(UIScrollView) -> () (ProductListController.swift:192)
15 Zattini                        0x10132de80 @objc ProductListController.scrollViewDidScroll(UIScrollView) -> () (ProductListController.swift)
16 UIKit                          0x18bd4270c -[UIScrollView(UIScrollViewInternal) _notifyDidScroll]
17 UIKit                          0x18ba5f090 -[UIScrollView setContentOffset:]
18 UIKit                          0x18bd46398 -[UIScrollView(UIScrollViewInternal) _adjustContentOffsetIfNecessary]
19 UIKit                          0x18ba99cc4 -[UIScrollView setBounds:]
20 UIKit                          0x18ba99580 -[UICollectionView setBounds:]
21 UIKit                          0x18bd1dcf4 -[UIView(Geometry) _applyISEngineLayoutValuesToBoundsOnly:]
22 UIKit                          0x18ba585c8 -[UIView(Geometry) _resizeWithOldSuperviewSize:]
23 UIKit                          0x18c5e4b48 -[UIScrollView(_UIOldConstraintBasedLayoutSupport) _resizeWithOldSuperviewSize:]
24 CoreFoundation                 0x1824e3a6c -[__NSArrayM enumerateObjectsWithOptions:usingBlock:]
25 UIKit                          0x18ba46af8 -[UIView(Geometry) resizeSubviewsWithOldSize:]
26 UIKit                          0x18bb2f018 -[UIView(AdditionalLayoutSupport) _is_layout]
27 UIKit                          0x18bd21228 -[UIView(Hierarchy) _updateConstraintsAsNecessaryAndApplyLayoutFromEngine]
28 UIKit                          0x18ba3e304 -[UIView(CALayerDelegate) layoutSublayersOfLayer:]
29 QuartzCore                     0x1865f7ec8 -[CALayer layoutSublayers]
30 QuartzCore                     0x1865fbfa8 CA::Layer::layout_if_needed(CA::Transaction*)
31 UIKit                          0x18ba530a8 -[UIView(Hierarchy) layoutBelowIfNeeded]
32 UIKit                          0x18bae8d7c -[UINavigationController _layoutViewController:]
33 UIKit                          0x18bae644c -[UINavigationController _layoutTopViewController]
34 UIKit                          0x18bdcf3c8 __105-[UINavigationController _repositionPaletteWithNavigationBarHidden:duration:shouldUpdateNavigationItems:]_block_invoke
35 UIKit                          0x18bdcf0a8 -[UINavigationController _repositionPaletteWithNavigationBarHidden:duration:shouldUpdateNavigationItems:]
36 UIKit                          0x18bae5a78 -[UINavigationController _updateBarsForCurrentInterfaceOrientation]
37 UIKit                          0x18bb67cac -[UITabBarController _updateLayoutForStatusBarAndInterfaceOrientation]
38 CoreFoundation                 0x18259c0bc __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__
39 CoreFoundation                 0x18259b65c _CFXRegistrationPost
40 CoreFoundation                 0x18259b3c0 ___CFXNotificationPost_block_invoke
41 CoreFoundation                 0x1826189c4 -[_CFXNotificationRegistrar find:object:observer:enumerator:]
42 CoreFoundation                 0x1824d20c0 _CFXNotificationPost
43 Foundation                     0x182ef3498 -[NSNotificationCenter postNotificationName:object:userInfo:]
44 UIKit                          0x18bf3f10c -[__UICanvasLifecycleMonitor_Compatability activateEventsOnly:withContext:completion:]
45 UIKit                          0x18c6ba4b8 __82-[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:]_block_invoke
46 UIKit                          0x18c6ba35c -[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:]
47 UIKit                          0x18c42c294 __125-    [_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]_block_invoke
48 UIKit                          0x18c5c30ac _performActionsWithDelayForTransitionContext
49 UIKit                          0x18c42c144 -[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]
50 UIKit                          0x18c2122d8 -[_UICanvas scene:didUpdateWithDiff:transitionContext:completion:]
51 UIKit                          0x18c0b4d68 -[UIApplicationSceneClientAgent scene:handleEvent:withCompletion:]
52 FrontBoardServices             0x184cb620c __80-[FBSSceneImpl updater:didUpdateSettings:withDiff:transitionContext:completion:]_block_invoke.362
53 libdispatch.dylib              0x181f8d048 _dispatch_client_callout
54 libdispatch.dylib              0x181f946c8 _dispatch_block_invoke_direct$VARIANT$mp
55 FrontBoardServices             0x184ce99f4 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__
56 FrontBoardServices             0x184ce9698 -[FBSSerialQueue _performNext]
57 FrontBoardServices             0x184ce9c34 -[FBSSerialQueue _performNextFromRunLoopSource]
58 CoreFoundation                 0x1825b22e8 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__
59 CoreFoundation                 0x1825b2268 __CFRunLoopDoSource0
60 CoreFoundation                 0x1825b1af0 __CFRunLoopDoSources0
61 CoreFoundation                 0x1825af6c8 __CFRunLoopRun
62 CoreFoundation                 0x1824cffb8 CFRunLoopRunSpecific
63 GraphicsServices               0x184367f84 GSEventRunModal
64 UIKit                          0x18baa42f4 UIApplicationMain
65 Zattini                        0x101113a84 main (AppDelegate.swift:21)
66 libdyld.dylib                  0x181ff256c start

上面的代码会导致

library(dplyr)
df = df %>% 
  mutate(species_id = tolower(as.character(species_id))) %>%
  group_by(species_id) %>%
  summarise(total = sum(count)) %>%
  ungroup()