[R] Using str() in a function.

Daniel Malter daniel at umd.edu
Thu Jul 14 07:23:12 CEST 2011


As long as you just want to display it, use print()

GG<- c(1,2,3) 
print(summary(GG),str(GG))


Output:
num [1:3] 1 2 3
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
    1.0     1.5     2.0     2.0     2.5     3.0 


HTH,
Daniel




andrewH wrote:
> 
> Using str() in a function.
> 
> I am in the early phase of learning R, and I find I spend a lot of time
> trying to figure out what is actually in objects I have created or read in
> from a file.  I'm trying to make a simple little function to display a
> couple of things about a object, let's say the summary() and the str(),
> sequentially, preferably without a bunch of surplus lines between them.  I
> have tried a large number of things; none do what I want. 
> 
>> GG<- c(1,2,3)
> # This one ignores the str().
>> testX <- function(X) {return(summary(X)); str(X)}
>> testX(GG)
>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
>     1.0     1.5     2.0     2.0     2.5     3.0 
> 
> # So does this one.
>> testX2 <- function(X) {return(summary(X)); return(str(X))}
>> testX2(GG)
>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
>     1.0     1.5     2.0     2.0     2.5     3.0 
> 
> # On the other hand, this one ignores the summary()
>> testX3 <- function(X) {summary(X); return(str(X))}
>> testX3(GG)
>  num [1:3] 1 2 3
> 
> # This one displays both, in reverse order, with a superfluous (to my
> intentions) [[NULL]].
>> testX4 <- function(X) {list(summary(X), (str(X)))}
>> testX4(GG)
>  num [1:3] 1 2 3
> [[1]]
>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
>     1.0     1.5     2.0     2.0     2.5     3.0 
> 
> [[2]]
> NULL
> 
> # Now we are back to ignoring the str().
>> testX5 <- function(X) {list(return(summary(X)), (str(X)))}
>> testX5(GG)
>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
>     1.0     1.5     2.0     2.0     2.5     3.0 
> 
> # This does the same as testX4().
>> testX6 <- function(X) {return(list(summary(X), (str(X))))}
>> testX6(GG)
>  num [1:3] 1 2 3
> [[1]]
>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
>     1.0     1.5     2.0     2.0     2.5     3.0 
> 
> [[2]]
> NULL
> 
> I tried a bunch more, using the print command, etc., but nothng I tried
> resulted in the output of summary() followed by the output of str(). And
> is there really no way to assign the output of str() -- that is to say,
> the output str() normally prints to the console -- to an object?  
> 
> I would be very greatful for any guidance you could offer.
> 
> Sincerely, Andrew
> 

--
View this message in context: http://r.789695.n4.nabble.com/Using-str-in-a-function-tp3655785p3666684.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list