在阅读以下帮助文件时,应该可以为列名添加前缀:
colnames(x, do.NULL = TRUE, prefix = "col")
Run Code Online (Sandbox Code Playgroud)
以下对我不起作用.我在这做错了什么?
m2 <- cbind(1,1:4)
colnames(m2, do.NULL = FALSE)
colnames(m2) <- c("x","Y")
colnames(m2) <- colnames(m2, prefix = "Sub_")
colnames(m2)
Run Code Online (Sandbox Code Playgroud) 有人可以解释为什么这有效:
d <- data.frame(x = 1:10, y = as.numeric(c(1:4,rep(NA,6))),z=rep(1:5,2))
ggplot(data = d, aes(x, y)) + geom_point() +facet_grid(z~.)
Run Code Online (Sandbox Code Playgroud)
当添加scales="free"
到facet_grid
错误时抛出:
d <- data.frame(x = 1:10, y = as.numeric(c(1:4,rep(NA,6))),z=rep(1:5,2))
ggplot(data = d, aes(x, y)) + geom_point() +facet_grid(z~.,scales="free")
# Error in seq.default(from = best$lmin, to = best$lmax, by = best$lstep) :
# 'from' must be of length 1
Run Code Online (Sandbox Code Playgroud)
可能它在scales
不自由时使用所有方面的最小值和最大值.何时scales
是空闲的,它不知道只包含缺失的方面要采用哪个值?
有解决方法吗?
我想向我的观众提供由slidify或Rstudio的R演示文稿创建的HTML5演示文稿的讲义.有什么建议?
根据dplyr
帮助文件,该sample_n
功能每组采样固定数量.
当我运行以下代码时,我希望每个tobgp和alcgp组合有两个样本,所以总共有32(4*4*2)行.但是只返回两行.
by_tobgp_alcgp <- esoph %>% group_by(tobgp,alcgp)
sample_n(by_tobgp_alcgp , 2)
# Source: local data frame [2 x 5]
# Groups: tobgp, alcgp
#
# agegp alcgp tobgp ncases ncontrols
# 10 25-34 80-119 10-19 0 1
# 50 55-64 0-39g/day 30+ 4 6
Run Code Online (Sandbox Code Playgroud)
它是否正确?有没有其他方法可以实现这一目标dplyr
?
我使用点函数来格式化创建的图中的文本标签ggplot2
.这在使用时工作正常aes
,但在使用时不能像预期的那样工作aes_string
.是否有解决方法使其适用aes_string
?
require(ggplot2)
# Define the format function
dot <- function(x, ...) {
format(x, ..., big.mark = ".", scientific = FALSE, trim = TRUE)
}
# Create dummy data
df <- data.frame(cbind(levels(iris$Species),c(10000000000,200000,30000)))
df$X2 <- as.numeric(as.character(df$X2))
# Works with aes
ggplot(iris) +
geom_bar(aes(Species,Sepal.Width),stat="identity") +
geom_text(data=df,aes(x=factor(X1),y=180,label=dot(X2)))
# Doesn't work with aes_string
ggplot(iris) +
geom_bar(aes(Species,Sepal.Width),stat="identity") +
geom_text(data=df,aes_string(x="X1",y=180,label=dot("X2")))
Run Code Online (Sandbox Code Playgroud) 当使用平滑样条拟合广义加性模型时,stargazer 只返回主效应,而不是您可以在 中看到的平滑项summary(pros.gam)
。观星者也可以归还这些吗?或者是否有其他功能或包可以完成这项工作?
library(ElemStatLearn)
library(mgcv)
library(stargazer)
pros.gam=gam(lpsa~s(lcavol)+s(lweight)+s(age)+s(lbph)+svi
+s(lcp)+gleason+s(pgg45),data=prostate)
summary(pros.gam) # Table should include the smooth terms that are visible here
stargazer(pros.gam,summary=TRUE)
Run Code Online (Sandbox Code Playgroud) 如何在downloadHandler
不重新定义的情况下调用使用被动函数创建的绘图?
非工作示例:
# Part of server.R
output$tgPlot <- renderPlot({
plot1 <-ggplot(iris[iris$Species==input$species,])+geom_point(aes(Sepal.Length ,Sepal.Width))
print(plot1)
} )
output$plotsave <- downloadHandler(
filename = 'plot.pdf',
content = function(file){
pdf(file = file, width=12, height=4)
tgPlot()
dev.off()
}
)
Run Code Online (Sandbox Code Playgroud)
为什么不能调用tgPlot()downloadHandler
?还有另外一种方法吗?
我正在尝试读取大量分隔为R的散列标记的txt文件.
例如:
Dora#58529#26/04/2012#
Dora#58529#26/04/2012#
Dora#58529#26/04/2012#
Dora#58529#26/04/2012#
Dora#58529#26/04/2012#
Dora#58529#26/04/2012#
Dora#58529#26/04/2012#
Dora#58529#26/04/2012#
Dora#58529#26/04/2012#
Run Code Online (Sandbox Code Playgroud)
当我尝试以下操作时,只加载第一列,可能是因为第一个哈希标记之后的所有内容都被解释为注释.
(df <- read.table("https://dl.dropboxusercontent.com/u/64191100/hashtagdel.txt",sep="#"))
Run Code Online (Sandbox Code Playgroud)
输出:
# V1
# 1 Dora
# 2 Dora
# 3 Dora
# 4 Dora
# 5 Dora
# 6 Dora
# 7 Dora
# 8 Dora
# 9 Dora
Run Code Online (Sandbox Code Playgroud)
我不想用另一个字符更改每个文件中的哈希标记(我没有创建文件).有人知道解决方法吗?