我正在努力使用data.frame列的变量标签.假设我有以下数据框(更大的数据框的一部分):
data <- data.frame(age = c(21, 30, 25, 41, 29, 33), sex = factor(c(1, 2, 1, 2, 1, 2), labels = c("Female", "Male")))
#
Run Code Online (Sandbox Code Playgroud)
我还有一个带有该数据框变量标签的命名向量:
var.labels <- c(age = "Age in Years", sex = "Sex of the participant")
Run Code Online (Sandbox Code Playgroud)
我想在分配变量标签var.labels在数据帧中的列data使用功能label从Hmisc包.我可以像这样一个接一个地做,然后检查结果:
> label(data[["age"]]) <- "Age in years"
> label(data[["sex"]]) <- "Sex of the participant"
> label(data)
age sex
"Age in years" "Sex of the participant"
Run Code Online (Sandbox Code Playgroud)
变量标签被指定为列的属性:
> attr(data[["age"]], "label")
[1] "Age in years"
> …Run Code Online (Sandbox Code Playgroud) 我正在使用cut函数将我的数据拆分为相同的bin,它可以完成工作,但我对它返回值的方式不满意.我需要的是箱子的中心而不是上端和下端.
我也尝试过使用cut2{Hmisc},这给了我每个箱子的中心,但是它在包含相同数量的观察的箱子中划分数据范围,而不是长度相同.
有人有解决方案吗?
我使用的是r 3.3.3,dplyr 0.7.4和Hmisc 4.1-1.我注意到我加载包的顺序会影响dplyr :: summaries函数是否有效.我知道以不同的顺序加载包会掩盖某些函数,但我使用package :: function()语法来避免这个问题.确切的问题围绕标记变量.我知道过去有过tidyverse和变量标签的问题,但似乎都没有解决为什么会出现这种特殊情况.
第一个有效的例子 - 我只加载Hmisc然后加载dplyr并且我能够汇总数据 -
#this works fine
library(Hmisc)
library(dplyr)
Hmisc::label(iris$Petal.Width) <- "Petal Width"
sumpct <- iris %>%
dplyr::group_by(Species) %>%
dplyr::summarise(med =median(Petal.Width),A40 = round(100*ecdf(Petal.Width)(.40),1),
A50 =round(100*ecdf(Petal.Width)(.50),1),
mns = mean(Petal.Width),
lowermean = mean(Petal.Width)-sd(Petal.Width),
lowermedian = median(Petal.Width) - sd(Petal.Width))
Run Code Online (Sandbox Code Playgroud)
以下第二个例子打破.我启动一个新的会话并在Hmisc之后加载tidyverse并仍然使用package :: function()语法,但这会引发错误:
summarise_impl(.data,dots)出错:评估错误:
x且labels必须是同一类型.
第二个例子:
###restart session
#this example does not work
library(Hmisc)
library(tidyverse)
Hmisc::label(iris$Petal.Width) <- "Petal Width"
sumpct <- iris %>%
dplyr::group_by(Species) %>%
dplyr::summarise(med =median(Petal.Width),A40 = round(100*ecdf(Petal.Width)(.40),1),
A50 =round(100*ecdf(Petal.Width)(.50),1),
mns = …Run Code Online (Sandbox Code Playgroud) 我用的时候
library(Hmisc)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) : there is no package called 'stringi'
Error: package 'ggplot2' could not be loaded
Run Code Online (Sandbox Code Playgroud)
同样,如果我使用
library(ggplot2)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) : there is no package called 'stringi'
Error: package or namespace load failed for 'ggplot2'
Run Code Online (Sandbox Code Playgroud)
我试过安装'stringi'install.packages("stringi")
但在某些时候,在安装过程中,我收到以下错误消息:
configure: error: in `/private/var/folders/pr/wdr5dvjj24bb4wwnjpg1hndc0000gr/T/RtmpeQ5pXk/R.INSTALL10b94a012cab/stringi':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details
ERROR: configuration failed for package 'stringi'
* removing '/Library/Frameworks/R.framework/Versions/3.2/Resources/library/stringi' …Run Code Online (Sandbox Code Playgroud) 我在加载和运行 Hmisc 包时遇到问题。安装时,出现以下错误;
library(Hmisc)
Loading required package: lattice
Loading required package: survival
Loading required package: Formula
Loading required package: ggplot2
Error: package or namespace load failed for ‘Hmisc’ in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]):
there is no package called ‘latticeExtra’
Run Code Online (Sandbox Code Playgroud)
我正在运行 Rstudio 3.5.3
我尝试更新所有软件包。效果流程是我有标记变量的代码,这些代码不再运行并给出以下错误。
label(data$facem_360_v1_timestamp)="Survey Timestamp"
Error in label(data$facem_360_v1_timestamp) = "Survey Timestamp" :
could not find function "label<-"
Run Code Online (Sandbox Code Playgroud)
任何建议将不胜感激。
我一直在努力学习使用Hmisc-package中的summary()函数来生成包含chisquared测试的crosstables.在这个董事会的帮助下,我几乎就在那里.我只是无法弄清楚如何获得行百分比而不是列百分比.
#Data:
v1 <- sample(letters[8:12],200,replace=TRUE)
v2 <- sample(letters[1:2],200,replace=TRUE)
month <- sample(month.name[7:9],200,replace=TRUE)
df <- data.frame(v1,v2,month)
#Table:
latex( summary( month ~ v1 + v2 , data=df, method="reverse" ,test=TRUE), exclude1=FALSE,file="",booktabs=TRUE,long=TRUE)
Run Code Online (Sandbox Code Playgroud)
哪个让我这个:

这让我获得了列百分比.我正在寻找一种方法来扭转它,所以我得到行百分比.我一直在搜索Hmisc文档中的"行"和"列"和"百分比",但没有运气.summary.formular()函数有可选的参数"fun"但是让我做它的行百分比......
请帮忙
我正在尝试将列标题和标题下方的水平线分组.当我执行以下操作时,它可以工作,
library(Hmisc)
data(mtcars)
latex(mtcars, file ='', cgroup = c("Group 1", "Group 2"), n.cgroup = c(5, 6))
Run Code Online (Sandbox Code Playgroud)

但是当我尝试删除rownames时,第1组和第2组下的行合并到同一行
library(Hmisc)
data(mtcars)
latex(mtcars, file ='', cgroup = c("Group 1", "Group 2"), n.cgroup = c(5, 6), rowname = NULL)
Run Code Online (Sandbox Code Playgroud)

有谁知道解决这个问题的方法?
我正在使用cut将数据划分为多个二进制文件,这样就可以生成垃圾箱(x1,x2].任何人都可以告诉我如何制作一个表达这些箱子作为箱子中点的新栏目?例如,使用以下数据帧:
structure(list(x = c(1L, 4L, 6L, 7L, 8L, 9L, 12L, 18L, 19L),
y = 1:9), .Names = c("x", "y"), class = "data.frame", row.names = c(NA,
-9L))
Run Code Online (Sandbox Code Playgroud)
我可以用
test$xRange <- cut(test$x, breaks=seq(0, 20, 5))
Run Code Online (Sandbox Code Playgroud)
给
x y xRange
1 1 1 (0,5]
2 4 2 (0,5]
3 6 3 (5,10]
4 7 4 (5,10]
5 8 5 (5,10]
6 9 6 (5,10]
7 12 7 (10,15]
8 18 8 (15,20]
9 19 9 (15,20]
Run Code Online (Sandbox Code Playgroud)
但我需要的结果应该是:
x y …Run Code Online (Sandbox Code Playgroud) library(ggplot2)
data(diamonds)
str(diamonds)
## 'data.frame': 53940 obs. of 10 variables:
## $ carat : num 0.23 0.21 0.23 0.29 0.31 0.24 0.24 0.26 0.22 0.23 ...
## $ cut : Ord.factor w/ 5 levels "Fair"<"Good"<..: 5 4 2 4 2 3 3 3 1 3 ...
## $ color : Ord.factor w/ 7 levels "D"<"E"<"F"<"G"<..: 2 2 2 6 7 7 6 5 2 5 ...
## $ clarity: Ord.factor w/ 8 levels "I1"<"SI2"<"SI1"<..: 2 3 5 4 2 6 …Run Code Online (Sandbox Code Playgroud) 首先,我会告诉你我正在尝试做大事,以防我错了.我有一个嵌套的表,我希望使用knitr将其作为LaTX表放在RStudio中.我很好,直到我尝试添加标题.我在tables插图(LINK)中尝试了第9页上的示例.
它没有标题,但当我添加标题时,它没有.它也适用于非表格对象.有趣的是,它latex.default可以工作,但会导致RStudio/knitr的编译PDF中的错误,并且latex无论如何都会从我读到的内容中调用; 加上桌子不再适当地四舍五入.我尝试过,latexTabular但这也不合适.
library(Hmisc); library(tables)
latex(head(mtcars), file="", caption="de") #works
x <- tabular( (Species + 1) ~ (n=1) + Format(digits=2)*
(Sepal.Length + Sepal.Width)*(mean + sd), data=iris )
latex(x, file="", caption="de") #no caption :(
Run Code Online (Sandbox Code Playgroud)
理想情况下,我希望能够\caption{de}输出,但无法弄清楚我哪里出错了.
如果它在这里是有用的输入和输出:
> latex(x, file="", caption="de", label="tab1")
\begin{tabular}{lccccc}
\hline
& & \multicolumn{2}{c}{Sepal.Length} & \multicolumn{2}{c}{Sepal.Width} \\
Species & n & mean & sd & mean & sd \\
\hline
setosa & $\phantom{0}50$ & $5.01$ & $0.35$ & …Run Code Online (Sandbox Code Playgroud)