2016-09-27 30 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编码