1

I want to show only parts of an array, that has a long variable name. So far I used:

# Data
k0100_m020_r05_h10000__aea000_aee0$cumsumData = c(-1:10)

# show only parts
k0100_m020_r05_h10000__aea000_aee0$cumsumData[k0100_m020_r05_h10000__aea000_aee0$cumsumData>0]

I wonder if there is a shorter form, since I have to repeat the variable name within the square brackets. Is there a shorter reference to the variable currently used? e.g.:

k0100_m020_r05_h10000__aea000_aee0$cumsumData[_self>0]
R_User
  • 10,682
  • 25
  • 79
  • 120
  • 3
    Why don't you call the array (is it really an array or do you mean a dataframe, because the `$` operator doesn't work with matrices/arrays as far as I am aware) something else with a short name? e.g. `mydat <- k0100_m020_r05_h10000__aea000_aee0`, then `mydat[mydat$cumsumData > 0 , ]` – Simon O'Hanlon Apr 15 '13 at 12:37
  • Related/duplicate: http://stackoverflow.com/q/7768686/271616 – Joshua Ulrich Apr 15 '13 at 13:45
  • I want to call the array without auxiliary variables. The more variables you have, the more confusing are the sourcecodes. – R_User Apr 16 '13 at 08:07

3 Answers3

2

Try subset.

subset(k0100_m020_r05_h10000__aea000_aee0, cumsumData > 0)
Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
1

If you are going to want to look into more than one column an apply approach might be useful:

k0100_m020_r05_h10000__aea000_aee0 <- as.data.frame(matrix(rnorm(25),ncol=5))

apply( k0100_m020_r05_h10000__aea000_aee0 , 2 , function(x) x [ x>0 ] )
#$V1
#[1] 1.5840356 0.2497689

#$V2
#[1] 0.4179352 1.6187143 0.8245239

#$V3
#[1] 0.09328116 0.20761514 0.29894603 2.14484045

#$V4
#[1] 0.4882152 0.4643691 0.5591095

#$V5
#[1] 1.0749748 0.5405291 0.4899797 1.0828021
Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
0

I am not sure whether this is what you want, but perhaps with() can help:

mask <- with(k0100_m020_r05_h10000__aea000_aee0, _self > 0) # gives you a boolean vector
k0100_m020_r05_h10000__aea000_aee0[mask]                    # gives you the result
Maxim.K
  • 4,120
  • 1
  • 26
  • 43