2016-09-27 29 views
0

中的順序我正在寫一個函數,它將給出給定數據集的dim()和str()。str()首先打印無論在R

JustfunFun <- function(.csv) { 
    csv <- read.csv(.csv) 
    dimVal <- dim(csv) 
    print("The dimension of the dataset is:") 
    strVal <- str(csv) 
    print("The structute of the dataset is:") 
    headVal <- head(csv) 
    return(list(dimVal, strVal, headVal)) 
} 

理想情況下,輸出必須先維度,結構第二,然後是數據集的頭部。

但產量如下:

> JustfunFun("tips.csv") 
[1] "The dimension of the dataset is:" 
'data.frame': 244 obs. of 8 variables: 
$ obs : int 1 2 3 4 5 6 7 8 9 10 ... 
$ totbill: num 17 10.3 21 23.7 24.6 ... 
$ tip : num 1.01 1.66 3.5 3.31 3.61 4.71 2 3.12 1.96 3.23 ... 
$ sex : Factor w/ 2 levels "F","M": 1 2 2 2 1 2 2 2 2 2 ... 
$ smoker : Factor w/ 2 levels "No","Yes": 1 1 1 1 1 1 1 1 1 1 ... 
$ day : Factor w/ 4 levels "Fri","Sat","Sun",..: 3 3 3 3 3 3 3 3 3 3 ... 
$ time : Factor w/ 2 levels "Day","Night": 2 2 2 2 2 2 2 2 2 2 ... 
$ size : int 2 3 3 2 4 4 2 4 2 2 ... 
[1] "The structute of the dataset is:" 
[1] "The head of the dataset is:" 
[[1]] 
[1] 244 8 

[[2]] 
NULL 

[[3]] 
    obs totbill tip sex smoker day time size 
1 1 16.99 1.01 F  No Sun Night 2 
2 2 10.34 1.66 M  No Sun Night 3 
3 3 21.01 3.50 M  No Sun Night 3 
4 4 23.68 3.31 M  No Sun Night 2 
5 5 24.59 3.61 F  No Sun Night 4 
6 6 25.29 4.71 M  No Sun Night 4 

> 

我該如何解決這個問題?

回答

1

str,就像print不會返回任何東西。你可以看到utils:::str.default的最後一行。最簡單的方法是嘗試嵌套str(即str(str(mtcars)))。

此功能應打印您想要的方式,並存儲數據。

JustfunFun <- function(.csv) { 
    csv <- read.csv(.csv) 
    dimVal <- dim(csv) 
    print("The dimension of the dataset is:") 
    print(dimVal) 
    print("The structute of the dataset is:") 
    strVal <- utils:::capture.output(str(csv)) 
    print(strVal) 
    print(head(csv)) 
    return(invisible(list(dimVal, strVal, head(csv)))) 
} 

例子:

write.csv(mtcars, "mtcars.csv", row.names = FALSE) 
a <- JustfunFun("mtcars.csv") 

結果:你已經寫在你的功能

[1] "The dimension of the dataset is:" 
[1] 32 11 
[1] "The structute of the dataset is:" 
[1] "'data.frame':\t32 obs. of 11 variables:"       
[2] " $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ..." 
[3] " $ cyl : int 6 6 4 6 8 6 8 4 4 6 ..."        
[4] " $ disp: num 160 160 108 258 360 ..."        
[5] " $ hp : int 110 110 93 110 175 105 245 62 95 123 ..."   
[6] " $ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ..." 
[7] " $ wt : num 2.62 2.88 2.32 3.21 3.44 ..."      
[8] " $ qsec: num 16.5 17 18.6 19.4 17 ..."       
[9] " $ vs : int 0 0 1 1 0 1 0 1 1 1 ..."        
[10] " $ am : int 1 1 1 0 0 0 0 0 0 0 ..."        
[11] " $ gear: int 4 4 4 3 3 3 3 4 4 4 ..."        
[12] " $ carb: int 4 4 1 1 2 1 4 2 2 4 ..." 
    mpg cyl disp hp drat wt qsec vs am gear carb 
1 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 
2 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 
3 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 
4 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 
5 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 
6 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 

str(a) 
$ : int [1:2] 32 11 
$ : chr [1:12] "'data.frame':\t32 obs. of 11 variables:" " $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ..." " $ cyl : int 6 6 4 6 8 6 8 4 4 6 ..." " $ disp: num 160 160 108 258 360 ..." ... 
$ :'data.frame': 6 obs. of 11 variables: 
    ..$ mpg : num [1:6] 21 21 22.8 21.4 18.7 18.1 
    ..$ cyl : int [1:6] 6 6 4 6 8 6 
    ..$ disp: num [1:6] 160 160 108 258 360 225 
    ..$ hp : int [1:6] 110 110 93 110 175 105 
    ..$ drat: num [1:6] 3.9 3.9 3.85 3.08 3.15 2.76 
    ..$ wt : num [1:6] 2.62 2.88 2.32 3.21 3.44 ... 
    ..$ qsec: num [1:6] 16.5 17 18.6 19.4 17 ... 
    ..$ vs : int [1:6] 0 0 1 1 0 1 
    ..$ am : int [1:6] 1 1 1 0 0 0 
    ..$ gear: int [1:6] 4 4 4 3 3 3 
    ..$ carb: int [1:6] 4 4 1 1 2 1 
-1

如果您只對顯示結果感興趣而不存儲它,您可以編寫一個函數,它不返回任何內容,而是打印尺寸,結構和數據頭。下面的代碼似乎可以做到這一點。

# Simulation of the data 
dat <- data.frame(obs=1:100,totbill=round(100*runif(100)),sex=sample(c("F","M"),100,replace=TRUE)) 

# Function 
dim.str.head <- function(dat){ 
    print("The dimension of the dataset is:") 
    print(dim(dat)) 
    print("The structure of the dataset is:") 
    print(str(dat)) 
    print("The first observations look like this:") 
    head(dat) 
} 

# Try the function 
dim.str.head(dat) 
+0

該函數採用CSV文件作爲輸入。你的解決方案似乎沒有這樣做。我不需要替代功能來完成相同的工作,我想要一個解決方法來獲得所需的輸出。 –

+0

輸入無關緊要(無論如何,您都會從read.csv()步驟中獲得數據框),並且該想法是提供一個任何人都可以運行的代碼,符合SO的指導原則。對不起,你不喜歡它。 – Roland

+0

你的回答給了我一個洞見。請看看上面的兩個答案,那就是我想要的。 –

1

一切都只是你需要通過命令capture.output捕捉str的事實是正確的。所以,下面就是你正在尋找的功能:

JustfunFun <- function(.csv) { 
    csv <- read.csv(.csv) 
    dimVal <- dim(csv) 
    strVal <- capture.output(str(csv)) 
    headVal <- head(csv) 
    return(list("The dimension of the dataset is:" = dimVal, 
       "The structute of the dataset is:" = strVal, 
       headVal)) 
} 

乾杯&快樂[R編碼