-1

There is a data.frame, I print it with sapply:

df <- data.frame(name=c("alice","becka","james","jeffery","john"),  
                 sex=c("F","M","M","F","M"),  
                 age=c(13,13,12,13,12),  
                 height=c(56.5,65.3,57.3,62.5,69),  
                 weight=c(84,98,83,84,99.5),stringsAsFactors = FALSE)  
sapply(df,function(x){print(x)})  

 [1] "alice"   "becka"   "james"   "jeffery" "john"     
 [1] "F" "M" "M" "F" "M"  
 [1] 13 13 12 13 12  
 [1] 56.5 65.3 57.3 62.5 69.0  
 [1] 84.0 98.0 83.0 84.0 99.5  
      name      sex age  height weight  
 [1,] "alice"   "F" "13" "56.5" "84"    
 [2,] "becka"   "M" "13" "65.3" "98"    
 [3,] "james"   "M" "12" "57.3" "83"    
 [4,] "jeffery" "F" "13" "62.5" "84"    
 [5,] "john"    "M" "12" "69"   "99.5"  

Why is the output not as follows:

 [1] "alice"   "becka"   "james"   "jeffery" "john"     
 [1] "F" "M" "M" "F" "M"  
 [1] 13 13 12 13 12  
 [1] 56.5 65.3 57.3 62.5 69.0  
 [1] 84.0 98.0 83.0 84.0 99.5  

Why is there a data.frame at the end as part of whole output?

       name      sex age  height weight  
 [1,] "alice"   "F" "13" "56.5" "84"    
 [2,] "becka"   "M" "13" "65.3" "98"    
 [3,] "james"   "M" "12" "57.3" "83"    
 [4,] "jeffery" "F" "13" "62.5" "84"    
 [5,] "john"    "M" "12" "69"   "99.5"   
joran
  • 169,992
  • 32
  • 429
  • 468
showkey
  • 482
  • 42
  • 140
  • 295
  • Please provide [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). And don't post screenshots. Especially if they are not accessible. – zero323 Nov 03 '13 at 03:21
  • 2
    Wow, this question now looks nothing like what DWin and Ananda answered... Also, I don't see any mention of "levels" in the question, but it is in the title. – Frank Nov 06 '13 at 01:24

2 Answers2

3

print returns a value, and sapply converts its result to a matrix if it can. In this case, it printed all the stuff you asked it to and then returned its value, a character matrix. Use invisible to hide the second step:

> invisible(sapply(df,function(x){print(x)}))
[1] "alice"   "becka"   "james"   "jeffery" "john"   
[1] "F" "M" "M" "F" "M"
[1] 13 13 12 13 12
[1] 56.5 65.3 57.3 62.5 69.0
[1] 84.0 98.0 83.0 84.0 99.5

The OP's desired "credible or official sources" can be found by example:

> a <- print('bah')
> a
[1] "bah"

EDIT: Whoops. The documentation at ?print.default does quite explicitly say that it returns its argument. (Thanks, @joran.) I would still expect a "Value" section mentioning it again, though...

Frank
  • 66,179
  • 8
  • 96
  • 180
  • 1
    The very first sentence in `?print` is "print prints its argument and returns it invisibly". – joran Nov 07 '13 at 01:42
  • @joran Thanks; edited. I'd rather see a redundant "Value" section restating it, since I am incapable of reading from the top of a document that I know has/should have a section just for the information I want. :) – Frank Nov 07 '13 at 03:13
  • 1
    I actually agree. Probably some people think that functions who's main purpose is their side effect don't need a Value section, but I would prefer having one, even if all it says is "returns NULL invisibly" or something. – joran Nov 07 '13 at 05:06
0

You should probably use skip = 13 to ignore the headers. Or use "China" as your starting line. You may be able to parse together a substitute set of column names afterward with pasting row9 to row 10 (or use month.abb). This will let you avoid complications of reading columns as factors.

x<- read.fwf(textConnection(ll[13:i2]), widths = c(20,-1,rep(c(7,-1),13)))

> str(x)
'data.frame':   39 obs. of  14 variables:
 $ V1 : Factor w/ 39 levels "All Other           ",..: 8 19 6 4 26 36 35 3 39 21 ...
 $ V2 : num  1268 1149 300 253 246 ...
 $ V3 : num  1279 1135 288 256 258 ...
 $ V4 : num  1276 1083 286 254 257 ...
IRTFM
  • 258,963
  • 21
  • 364
  • 487