[英]Subsetting data frame by factor level
I have a big data frame with state names in one colum and different indexes in the other columns. 我有一个大数据框,其中一个列中包含状态名,另一列中包含不同的索引。 I want to subset by state and create an object suitable for minimization of the index or a data frame with the calculation already given. 我希望按状态进行子集化,并创建一个适合于最小化索引的对象或已经给出计算的数据框。
Here's one simple (short) example of what I have 这是我所拥有的一个简单(简短)的例子
m
x y
1 A 1.0
2 A 2.0
3 A 1.5
4 B 3.0
5 B 3.5
6 C 7.0
I want to get this 我想得到这个
m
x y
1 A 1.0
2 B 3.0
3 C 7.0
I don't know if a function with a for loop is necessary. 我不知道是否需要带有for循环的函数。 Like 喜欢
minimize<-function(x,...)
for (i in m$x){
do something with data by factor value
apply to that something the min function in every column
return(y)
}
so when you call 所以当你打电话
minimize(A)
[1] 1
I tried to use %in% but didn't work (I got this error). 我尝试使用%in%但没有工作(我收到此错误)。
A%in%m Error in match(x, table, nomatch = 0L) : object 'A' not found %in%m匹配错误(x,table,nomatch = 0L):找不到对象'A'
When I define it it goes like this. 当我定义它时,它就像这样。
A<-c("A")
"A"%in%m
[1] FALSE
Thank you in advance 先感谢您
Use aggregate
使用aggregate
> aggregate(.~x, FUN=min, dat)
x y
1 A 1
2 B 3
3 C 7
See this post to get some other alternatives. 看到这篇文章,以获得一些其他选择。
Try aggregate
: 尝试aggregate
:
aggregate(y ~ x, m, min)
x y
1 A 1
2 B 3
3 C 7
Using data.table
使用data.table
require(data.table)
m <- data.table(m)
m[, j=min(y), by=x]
# x V1
# 1: A 1
# 2: B 3
# 3: C 7
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.