中的顺序我正在写一个函数,它将给出给定数据集的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
>
我该如何解决这个问题?
该函数采用CSV文件作为输入。你的解决方案似乎没有这样做。我不需要替代功能来完成相同的工作,我想要一个解决方法来获得所需的输出。 –
输入无关紧要(无论如何,您都会从read.csv()步骤中获得数据框),并且该想法是提供一个任何人都可以运行的代码,符合SO的指导原则。对不起,你不喜欢它。 – Roland
你的回答给了我一个洞见。请看看上面的两个答案,那就是我想要的。 –