[英]How to specific rows from a split list in R based on column condition
I am new to R and to programming in general and am looking for feedback on how to approach what is probably a fairly simple problem in R. 我是R和一般编程人员的新手,我正在寻找有关如何处理R中可能相当简单的问题的反馈。
I have the following dataset: 我有以下数据集:
df <- data.frame(county = rep(c("QU","AN","GY"), 3),
park = (c("Downtown","Queens", "Oakville","Squirreltown",
"Pinhurst", "GarbagePile","LottaTrees","BigHill",
"Jaynestown")),
hectares = c(12,42,6,18,92,6,4,52,12))
df<-transform(df, parkrank = ave(hectares, county,
FUN = function(x) rank(x, ties.method = "first")))
Which returns a dataframe looking like this: 它返回如下所示的数据框:
county park hectares parkrank
1 QU Downtown 12 2
2 AN Queens 42 1
3 GY Oakville 6 1
4 QU Squirreltown 18 3
5 AN Pinhurst 92 3
6 GY GarbagePile 6 2
7 QU LottaTrees 4 1
8 AN BigHill 52 2
9 GY Jaynestown 12 3
I want to use this to create a two-column data frame that lists each county and the park name corresponding to a specific rank (eg if when I call my function I add "2" as a variable, shows the second biggest park in each county). 我想用它来创建一个两列的数据框,该框列出每个县和对应于特定等级的公园名称(例如,当我调用函数时,将“ 2”添加为变量,显示每个公园中第二大的公园)县)。
I am very new to R and programming and have spent hours looking over the built in R help files and similar questions here on stack overflow but I am clearly missing something. 我对R和编程非常陌生,花了数小时在堆栈溢出中查看内置的R帮助文件和类似问题,但我显然缺少了一些东西。 Can anyone give a simple example of where to begin?
谁能给一个简单的例子,从哪里开始? It seems like I should be using split then lapply or maybe tapply, but everything I try leaves me very confused :(
似乎我应该先使用split然后再使用lapply或tapply,但是我尝试的所有操作都让我很困惑:(
Thanks. 谢谢。
Try, 尝试,
df2 <- function(A,x) {
# A is the name of the data.frame() and x is the rank No
df <- A[A[,4]==x,]
return(df)
}
> df2(df,2)
county park hectares parkrank
1 QU Downtown 12 2
6 GY GarbagePile 6 2
8 AN BigHill 52 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.