有条件地对R中的数据帧列表进行子集化

时间:2014-01-28 22:47:04

标签: r list conditional-statements subset

我有一个名为myList的数据帧列表(参见下面的示例),我想要的是按照只有“pointNum”>行的条件对数据帧列表进行子集化。 100列在新列表中。应该很容易,但我无法让它工作。因此,对于列表中的第一项,输出应该如下所示:

[[1]]
     study Identi    locDate locNumb          meanLat           meanLon pointNum
5  study 1 SDU101 2011-07-13      49 32.8837771221667  -117.24038866075      120
9  study 1 SDU101 2011-07-13      60 32.8838778530086 -117.240522195673      349
11 study 1 SDU101 2011-07-13     321 32.8027296698536 -117.210527201581      683

我一直试图让这个工作,以及其他类似的子集选项。它目前运行但没有做任何事情:

newList = lapply(myList, function(x) { subset(x, "pointNum" > 2)} )

我知道已经发布了类似的问题,但我无法获得任何解决方案来解决我的特定问题。任何帮助将不胜感激。

myList <- list(structure(list(study = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Study 1", class = "factor"), 
Identi = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L), .Label = "SDU101", class = "factor"), 
locDate = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L), .Label = "2011-07-13", class = "factor"), 
locNumb = structure(c(12L, 15L, 1L, 2L, 8L, 9L, 10L, 11L, 
13L, 14L, 3L, 4L, 5L, 6L, 7L), .Label = c("10", "11", "321", 
"323", "324", "326", "329", "49", "56", "57", "59", "6", 
"60", "61", "7"), class = "factor"), meanLat = structure(c(11L, 
10L, 4L, 9L, 6L, 8L, 3L, 5L, 7L, 12L, 1L, 15L, 13L, 14L, 
2L), .Label = c("32.8027296698536", "32.802755201875", "32.883244695", 
"32.8835599674286", "32.8837003266667", "32.8837771221667", 
"32.8838778530086", "32.88411147", "32.88419565", "32.8841969254545", 
"32.884720435", "32.8853723146154", "32.8853777533333", "32.8854051", 
"32.9164754136842"), class = "factor"), meanLon = structure(c(13L, 
10L, 12L, 15L, 9L, 8L, 7L, 4L, 11L, 6L, 2L, 3L, 14L, 5L, 
1L), .Label = c("-117.210382870833", "-117.210527201581", 
"-117.236141991053", "-117.239834913333", "-117.23989078", 
"-117.240133633077", "-117.240140015", "-117.24022087", "-117.24038866075", 
"-117.240416713636", "-117.240522195673", "-117.240532619714", 
"-117.24062533", "-117.24063566", "-117.24070002"), class = "factor"), 
pointNum = structure(c(6L, 2L, 9L, 1L, 3L, 1L, 6L, 7L, 8L, 
4L, 11L, 5L, 7L, 1L, 10L), .Label = c("1", "11", "120", "13", 
"19", "2", "3", "349", "35", "48", "683"), class = "factor")), .Names = c("study", 
"Identi", "locDate", "locNumb", "meanLat", "meanLon", "pointNum"
), row.names = c(NA, -15L), class = "data.frame"), structure(list(
study = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L), .Label = "Study 1", class = "factor"), 
Identi = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L), .Label = "SDU111", class = "factor"), 
locDate = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L), .Label = "2011-07-12", class = "factor"), 
locNumb = structure(c(14L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 11L, 12L, 13L), .Label = c("354", "418", "419", 
"420", "421", "422", "425", "426", "427", "428", "429", "430", 
"432", "67"), class = "factor"), meanLat = structure(c(2L, 
3L, 9L, 1L, 5L, 8L, 13L, 11L, 6L, 7L, 10L, 12L, 14L, 4L), .Label = c("32.8651107616667", 
"32.86543857", "32.867004565", "32.868283279", "32.868857725", 
"32.869014345", "32.8692111971429", "32.8693627126536", "32.8694241808955", 
"32.8694814566667", "32.86955278", "32.8696187847619", "32.8696329253571", 
"32.8698972233333"), class = "factor"), meanLon = structure(c(13L, 
12L, 8L, 14L, 11L, 2L, 7L, 5L, 4L, 1L, 3L, 9L, 6L, 10L), .Label = c("-117.235456126857", 
"-117.235585179972", "-117.235959423333", "-117.25006813", 
"-117.25014399", "-117.250450876667", "-117.250467514464", 
"-117.25050148", "-117.250773722857", "-117.2512085715", 
"-117.25133879", "-117.25283091", "-117.254194355", "-117.254406255417"
), class = "factor"), pointNum = structure(c(2L, 2L, 11L, 
5L, 2L, 8L, 9L, 1L, 2L, 7L, 6L, 4L, 10L, 3L), .Label = c("1", 
"2", "20", "21", "24", "3", "35", "358", "56", "6", "67"), class = "factor")), .Names = c("study", 
"Identi", "locDate", "locNumb", "meanLat", "meanLon", "pointNum"
), row.names = c(NA, -14L), class = "data.frame"))

1 个答案:

答案 0 :(得分:3)

您有两个问题 - 额外引号和pointNum中的dput是一个因素,所以这样做:

lapply(myList, function(x) { subset(x, as.integer(as.character(pointNum)) > 2)} )