比较同一R数据框中的行

时间:2019-04-30 06:31:16

标签: r dataframe

我想获取数据帧中的第n行,并将其与非第n行的所有行进行比较,并返回多少列匹配和/或不匹配。

我为单个观测值尝试了match函数和ifelse,但是我无法在整个数据帧中复制它。

数据集Superstore包含订单优先级,客户名​​称,发货方式,客户群和类别。看起来像这样:

graphqlX(schema, '{ hello }', root).then((response) => {
    console.log(response)
})

我尝试的代码(提取相关列):

GIT_SSH_COMMAND

这将返回:

courses.aggregate(
    [
        { 
            "$unwind" : {
                "path" : "$allotment"
            }
        }, 
        { 
            "$lookup" : {
                "from" : "subjects", 
                "localField" : "allotment.subject", 
                "foreignField" : "_id", 
                "as" : "allotment.subject"
            }
        }, 
        { 
            "$lookup" : {
                "from" : "teachers", 
                "localField" : "allotment.teacher", 
                "foreignField" : "_id", 
                "as" : "allotment.teacher"
            }
        }, 
        { 
            "$addFields" : {
                "allotment.subject" : {
                    "$arrayElemAt" : [
                        "$allotment.subject", 
                        0.0
                    ]
                }, 
                "allotment.teacher" : {
                    "$arrayElemAt" : [
                        "$allotment.teacher", 
                        0.0
                    ]
                }
            }
        }, 
        { 
            "$group" : {
                "_id" : "$_id", 
                "isAssigned" : {
                    "$first" : "$isAssigned"
                }, 
                "name" : {
                    "$first" : "$name"
                }, 
                "section" : {
                    "$first" : "$section"
                }, 
                "allotment" : {
                    "$addToSet" : "$allotment"
                }
            }
        }
    ]
)

使用ifelse,我得到:

> head(df2)
  Order.Priority     Customer.Name      Ship.Mode Customer.Segment Product.Category
1  Not Specified       Dana Teague    Regular Air        Corporate  Office Supplies
2       Critical     Vanessa Boyer    Regular Air         Consumer  Office Supplies
3       Critical       Wesley Tate    Regular Air        Corporate       Technology
4           High       Brian Grady Delivery Truck        Corporate        Furniture
5         Medium Kristine Connolly Delivery Truck        Corporate        Furniture
6           High       Emily Britt    Regular Air        Corporate  Office Supplies

就像我说的那样,这正是我需要的结果,但是我无法复制整个数据帧。

0 个答案:

没有答案