中的順序我正在寫一個函數,它將給出給定數據集的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
你的回答給了我一個洞見。請看看上面的兩個答案,那就是我想要的。 –