我目前正在Coursera上做一个可重复数据课程,其中一个问题是每天步骤的平均值和中位数,我有这个但是当我用摘要函数确认它时,Mean和Median的摘要版本是不同的.我是通过knitr来运行的
为什么会这样?**下面是一个编辑,显示到目前为止我的所有脚本,包括原始数据的链接:
##Download the data You have to change https to http to get this to work in knitr
target_url <- "http://d396qusza40orc.cloudfront.net/repdata%2Fdata%2Factivity.zip"
target_localfile = "ActivityMonitoringData.zip"
if (!file.exists(target_localfile)) {
download.file(target_url, destfile = target_localfile)
}
Unzip the file to the temporary directory
unzip(target_localfile, exdir="extract", overwrite=TRUE)
List the extracted files
list.files("./extract")
## [1] "activity.csv"
Load the extracted data into R
activity.csv <- read.csv("./extract/activity.csv", header = TRUE)
activity1 <- activity.csv[complete.cases(activity.csv),]
str(activity1)
## 'data.frame': 15264 obs. of 3 variables:
## $ steps : int 0 …Run Code Online (Sandbox Code Playgroud) 我的一些代码存在问题.我收到的dd/mm/yyyy格式为日期,称为字符串dateofQ.
我想要日期yyyy_mm_dd,我正在使用一个string.split()数组,但它不会返回名为myArr [3]的第三个数组:
String[] myArr = dateofQ.split("\\/");
String dateFormat = String.format("%s_%s_%s",myArr[2],myArr[1],myArr[0]);
Run Code Online (Sandbox Code Playgroud)
它返回myArr[1]和myArr[0],但是当我还加myArr[3]我在运行时得到一个问题:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at ReadFile.main(ReadFile.java:34)
Run Code Online (Sandbox Code Playgroud) 我在R中创建了一个函数来创建一个提供计数和百分比的表:
tblFun <- function(x){
tbl <- table((x))
res <- cbind(tbl,round(prop.table(tbl)*100,0))
colnames(res) <- c('Count','Percentage')
res}
Run Code Online (Sandbox Code Playgroud)
然后执行它我在我的数据集中的一个字段运行它并使用kable输出:
region <-tblFun(mtcars$mpg)
knitr::kable(region)
Run Code Online (Sandbox Code Playgroud)
这给出了一个按因子名称排序的表,但是我想按计数或百分比排序.

我已经尝试过我所知道的排序功能.我无法使用tidyverse库函数,因为它们不会给我正确的百分比:
library(dplyr)
region <- na.omit(mtcars) %>%
group_by(mtcars$mpg) %>%
summarize(Count=n()) %>%
mutate(Percent = round((n()/sum(n())*100))) %>%
arrange(desc(Count))
knitr::kable(region)
Run Code Online (Sandbox Code Playgroud)
任何人的修复将不胜感激.