按主题ID和dplyr

时间:2017-09-28 18:23:39

标签: r dplyr

我有两个数据集,我想加入一个数据集:

set.seed(111)
dat1 = data.frame(subject = paste0("A-",c(1,1,2,2,3,3,3,4,4,5)), 
                  seq1 = c(2, 3, 7, 8, 2, 9, 11, 10, 11, 16), 
                  text1 = sample(LETTERS, 10))
dat1

   subject seq1 text1
1      A-1    2     P
2      A-1    3     S
3      A-2    7     I
4      A-2    8     L
5      A-3    2     X
6      A-3    9     V
7      A-3   11     A
8      A-4   10     K
9      A-4   11     H
10     A-5   16     B

set.seed(999)
dat2 = data.frame(subject = paste0("A-", c(1,1,1,2,2,3,3,3,4,5,5,5,5)), 
                  seq2 = c(2, 3, 4, 7, 8, 2, 9, 11, 10, 16, 17, 18, 19), 
                  text2 = sample(LETTERS, 13))
dat2
       subject seq2 text2
1      A-1    2     K
2      A-1    3     O
3      A-1    4     C
4      A-2    7     T
5      A-2    8     R
6      A-3    2     X
7      A-3    9     M
8      A-3   11     B
9      A-4   10     H
10     A-5   16     Z
11     A-5   17     E
12     A-5   18     I
13     A-5   19     A

两个数据集都与第一列中的subject相同,但每个subject的观察数可能不同(例如,在dat1中,主题A-1有两个记录(seq1有2和3),但在dat2中,同一主题A-1有三条记录(seq2有2,3和4)。我想加入数据集包含两个数据集中的所有主题,同时在同一行中保留唯一的序列号(seq1seq2)以及相应的text1text2。分配{{ 1}}如果一个数据集缺少一个序列ID,但在另一个数据集中找到相同的序列ID。

最终数据集如下:

NA

如您所见,联合数据集对于相同的# subject seq1 text1 seq2 text2 # 1 A-1 2 P 2 K # 2 A-1 NA NA 4 C # 3 A-1 3 S 3 O # 4 A-2 7 I 7 T # 5 A-2 8 L 8 R # 6 A-3 2 X 2 X # 7 A-3 9 V 9 M # 8 A-3 11 A 11 B # 9 A-4 10 K 10 H # 10 A-4 11 H NA NA # 11 A-5 16 B 16 Z # 12 A-5 17 E NA NA # 13 A-5 18 I NA NA # 14 A-5 19 A NA NA 具有相同的seq1seq2,并且如果缺少序列ID(在subjectseq1),已分配seq2。感谢您使用NA完成此任务的建议!

2 个答案:

答案 0 :(得分:5)

我在每个数据集中创建了一个名为seq的新列,然后使用了full_join

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    let branch: Branch = Branch.getInstance()
    branch.initSession(launchOptions: launchOptions, andRegisterDeepLinkHandler: {params, error in

        DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {

            let alert = UIAlertController(title: "Branch", message: "\(params as? [String: AnyObject])", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
            self.window?.rootViewController?.present(alert, animated: false, completion: nil)
        })

        if error == nil {
            // params are the deep linked params associated with the link that the user clicked -> was re-directed to this app
            // params will be empty if no data found
            // TODO: ... insert custom logic here ...
            print("params: %@", params as? [String: AnyObject] ?? {})
        }
    })
    ...
    // facebook SDK login integration
    return SDKApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
}

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
    // pass the url to the handle deep link call
    let branchHandled = Branch.getInstance().application(application,
                                                         open: url,
                                                         sourceApplication: sourceApplication,
                                                         annotation: annotation)
    return branchHandled
}

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    // pass the url to the handle deep link call
    let branchHandled = Branch.getInstance().application(app,
                                                         open: url,
                                                         options: options)
    return branchHandled
}

答案 1 :(得分:2)


%10.0g