4

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. 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).

A%in%m Error in match(x, table, nomatch = 0L) : object 'A' not found

When I define it it goes like this.

A<-c("A")
"A"%in%m
[1] FALSE

Thank you in advance

Matias Andina
  • 4,029
  • 4
  • 26
  • 58
  • If you need to use this subsetting BE CAREFUL!! FIRST check if there's any NA couse min function will mess with it and give you wrong minimum values – Matias Andina Jan 22 '14 at 01:29

3 Answers3

3

Use 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.

Community
  • 1
  • 1
Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
2

Try aggregate:

aggregate(y ~ x, m, min)

  x y
1 A 1
2 B 3
3 C 7
Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
  • Thank you so much!!! I am so new at R it took me 2 hours searching for this answer here, nobody used aggregate. In fact I cuouldn't find the other post with those alternatives before now – Matias Andina Jan 21 '14 at 19:22
1

Using 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
marbel
  • 7,560
  • 6
  • 49
  • 68