根据三个其他变量之间的比较创建新变量

时间:2014-03-26 14:59:10

标签: r

我想基于三列之间的行方式比较在我的数据框架中创建一个新列。示例数据框:

df <- structure(list(year = c(1986L, 1987L, 1991L, 1991L, 1991L, 1991L, 1992L, 1992L, 1992L, 1992L, 1992L, 1992L, 1993L, 1993L, 1993L, 1993L, 1993L, 1993L, 1993L, 1993L, 1993L, 1993L, 1993L, 1993L, 1993L, 1993L, 1993L, 1993L, 1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 1994L), knmilocatie = structure(c(4L, 16L, 10L, 12L, 9L, 20L, 12L, 12L, 25L, 9L, 30L, 26L, 22L, 18L, 15L, 24L, 13L, 31L, 27L, 5L, 3L, 19L, 21L, 23L, 20L, 26L, 26L, 31L, 35L, 25L, 11L, 28L, 8L, 29L, 36L, 34L, 7L, 28L, 17L, 14L, 33L, 1L, 11L, 6L, 32L, 27L, 29L, 20L, 20L, 2L), .Label = c("Achterdiep", "Annen", "Appingedam", "Assen", "Bedum", "De Klip", "Delfzijl", "Eenrum", "Eleveld", "Emmen", "Garsthuizen", "Geelbroek", "Haren", "Hellum", "Hoogezand", "Hooghalen", "Kolham", "Langelo", "Loppersum", "Middelstum", "Nijenklooster", "Noordbroek", "Oldenzijl", "Overschild", "Roswinkel", "Slochteren", "Stedum", "Steendam", "t-Zandt", "Ten Boer", "Ten Post", "Uithuizermeeden", "Weiwerd", "Westerbroek", "Winneweer", "Zandeweer"), class = "factor"), baglocatie = structure(c(2L, 12L, 5L, 4L, 2L, 17L, 11L, 2L, 21L, 2L, 16L, 35L, 27L, 14L, 22L, 19L, 33L, 34L, 26L, 17L, 1L, 18L, 1L, 28L, 6L, 25L, 25L, 29L, 9L, 21L, 10L, 19L, 34L, 15L, 36L, 13L, 7L, 19L, 8L, 23L, 7L, 31L, 17L, 1L, 20L, 3L, 10L, 32L, 30L, 24L), .Label = c("Appingedam", "Assen", "Bedum", "Ekehaar", "Emmen", "Eppenhuizen", "Farmsum", "Froombosch", "Garrelsweer", "Garsthuizen", "Geelbroek", "Hooghalen", "Kolham", "Langelo", "Leermens", "Loppersum", "Middelstum", "Oosterwijtwerd", "Overschild", "Roodeschool", "Roswinkel", "Sappemeer", "Schildwolde", "Schipborg", "Slochteren", "Stedum", "Steendam", "t-Zandt", "Ten Post", "Toornwerd", "Tripscompagnie", "Warffum", "Westerbroek", "Wirdum", "Woudbloem", "Zandeweer"), class = "factor"), lllocatie = structure(c(3L, 13L, 5L, 10L, 4L, 32L, 10L, 10L, 22L, 4L, 36L, 37L, 31L, 15L, 23L, 20L, 34L, 8L, 24L, 35L, 19L, 19L, 2L, 29L, 26L, 25L, 25L, 30L, 8L, 22L, 9L, 20L, 19L, 16L, 38L, 12L, 6L, 27L, 7L, 11L, 17L, 33L, 14L, 2L, 21L, 18L, 9L, 28L, 32L, 1L), .Label = c("Annen", "Appingedam", "Assen", "Eleveld", "Emmen", "Farmsum", "Froombosch", "Garrelsweer", "Garsthuizen", "Geelbroek", "Hellum", "Hoogezand", "Hooghalen", "Huizinge", "Langelo", "Leermens", "Meedhuizen", "Onderdendam", "Oosterwijtwerd", "Overschild", "Roodeschool", "Roswinkel", "Sappemeer", "Sint Annen", "Slochteren", "Startenhuizen", "Steendam", "Stitswerd", "t-Zandt", "Ten Post", "Tjuchem", "Toornwerd", "Tripscompagnie", "Westerbroek", "Westerwijtwerd", "Winneweer", "Woudbloem", "Zandeweer"), class = "factor")), .Names = c("year", "knmilocatie", "baglocatie", "lllocatie"), class = "data.frame", row.names = c(NA, -50L))

当三列("knmilocatie", "baglocatie", "lllocatie")的值在一行中相同时(如示例数据帧的前三行中的情况),新列应获取值{{1}对于该行,表示只有一个唯一值的事实。当所有三列具有不同的值时(请参阅第11行),新列应获得该行的值1。如果比较中有两个唯一值,则应获取值3

有任何建议如何实现?

1 个答案:

答案 0 :(得分:1)

两种可能性:

apply(df[,-1], 1, function(u) length(unique(u))) # as you corrected it
colSums(!apply(df[,-1], 1, duplicated))

如果您拥有大量数据集,第二种可能性会更快。 HTH