我想查看一个函数的源代码,看看它是如何工作的.我知道我可以通过在提示符下键入其名称来打印函数:
> t
function (x)
UseMethod("t")
<bytecode: 0x2332948>
<environment: namespace:base>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,是什么UseMethod("t")
意思?我如何找到实际使用的源代码,例如:t(1:10)
?
有没有当我看到之间的差异UseMethod
,当我看到standardGeneric
和showMethods
,与with
?
> with
standardGeneric for "with" defined from package "base"
function (data, expr, ...)
standardGeneric("with")
<bytecode: 0x102fb3fc0>
<environment: 0x102fab988>
Methods may be defined for arguments: data
Use showMethods("with") for currently available ones.
Run Code Online (Sandbox Code Playgroud)
在其他情况下,我可以看到正在调用R函数,但我找不到这些函数的源代码.
> ts.union
function (..., dframe = FALSE)
.cbind.ts(list(...), .makeNamesTs(...), dframe = dframe, union = TRUE)
<bytecode: 0x36fbf88>
<environment: namespace:stats>
> .cbindts
Error: object '.cbindts' …
Run Code Online (Sandbox Code Playgroud) 假设我有一个两个单词的字符串,我想把它们都大写.
name <- c("zip code", "state", "final count")
Run Code Online (Sandbox Code Playgroud)
这个Hmisc
包有一个capitalize
大写第一个单词的函数,但我不确定如何将第二个单词大写.帮助页面capitalize
并不表示它可以执行该任务.
library(Hmisc)
capitalize(name)
# [1] "Zip code" "State" "Final count"
Run Code Online (Sandbox Code Playgroud)
我想得到:
c("Zip Code", "State", "Final Count")
Run Code Online (Sandbox Code Playgroud)
三字串怎么样:
name2 <- c("I like pizza")
Run Code Online (Sandbox Code Playgroud) 应该何时使用a data.frame
,何时使用matrix
?
两者都以矩形格式保存数据,因此有时候还不清楚.
有什么时候使用哪种数据类型有一般的经验法则吗?
我正在寻找相反的get()
.
给定一个对象名称,我希望有一个表示该对象的字符串直接从该对象中提取.
foo
作为我正在寻找的功能的占位符的简单示例.
z <- data.frame(x=1:10, y=1:10)
test <- function(a){
mean.x <- mean(a$x)
print(foo(a))
return(mean.x)}
test(z)
Run Code Online (Sandbox Code Playgroud)
会打印:
"z"
Run Code Online (Sandbox Code Playgroud)
我的工作,在我目前的问题中更难实现的是:
test <- function(a="z"){
mean.x <- mean(get(a)$x)
print(a)
return(mean.x)}
test("z")
Run Code Online (Sandbox Code Playgroud) 我是R的新手,但是我已经制作了许多具有较小数据集的相关图.但是,当我尝试绘制一个大型数据集(2gb +)时,我可以很好地生成绘图,但图例不会显示.有什么建议?还是替代品?
library(gplots)
r.cor <- cor(r)
layout(matrix(c(1,1,1,1,1,1,1,1,2,2), 5, 2, byrow = TRUE))
par(oma=c(5,7,1,1))
cx <- rev(colorpanel(25,"yellow","black","blue"))
leg <- seq(min(r.cor,na.rm=T),max(r.cor,na.rm=T),length=10)
image(r.cor,main="Correlation plot Normal/Tumor data",axes=F,col=cx)
axis(1, at=seq(0,1,length=ncol(r.cor)), labels=dimnames(r.cor)[[2]],
cex.axis=0.9,las=2)
axis(2,at=seq(0,1,length=ncol(r.cor)), labels=dimnames(r.cor)[[2]],
cex.axis=0.9,las=2)
image(as.matrix(leg),col=cx,axes=T)
Run Code Online (Sandbox Code Playgroud)
错误
plot.new()
:图边距太大
tmp <- round(leg,2)
axis(1,at=seq(0,1,length=length(leg)), labels=tmp,cex.axis=1)
Run Code Online (Sandbox Code Playgroud) 文件说
vapply
类似于sapply
,但具有预先指定的返回值类型,因此使用起来更安全.
你能详细说明为什么它通常更安全,可能提供例子吗?
PS:我知道答案,我已经倾向于避免sapply
.我只希望在这里有一个很好的答案,所以我可以指出我的同事.请不要"阅读手册"的答案.
我如何使用strptime
或任何其他函数来解析R中的毫秒时间戳?
time[1]
# [1] "2010-01-15 13:55:23.975"
strptime(time[1], format="%Y-%m-%d %H:%M:%S.%f")
# [1] NA
strptime(time[1], format="%Y-%m-%d %H:%M:%S")
# [1] "2010-01-15 13:55:23"`
Run Code Online (Sandbox Code Playgroud) 所以这是一个微不足道的问题,但是我无法回答这个问题,也许答案会告诉我一些关于R如何工作的更多细节.
标题说明了一切:R如何解析->
,模糊的右侧赋值函数?
我常用的技巧是失败:
`->`
Run Code Online (Sandbox Code Playgroud)
错误:
->
找不到对象
getAnywhere("->")
Run Code Online (Sandbox Code Playgroud)
没有
->
找到任何对象
我们不能直接称它为:
`->`(3,x)
Run Code Online (Sandbox Code Playgroud)
错误:无法找到功能
"->"
但当然,它有效:
(3 -> x) #assigns the value 3 to the name x
# [1] 3
Run Code Online (Sandbox Code Playgroud)
似乎R知道如何简单地反转论点,但我认为上述方法肯定会破解这种情况:
pryr::ast(3 -> y)
# \- ()
# \- `<- #R interpreter clearly flipped things around
# \- `y # (by the time it gets to `ast`, at least...)
# \- 3 # (note: this is because `substitute(3 -> y)`
# # already returns the reversed version)
Run Code Online (Sandbox Code Playgroud)
将此与常规赋值运算符进行比较: …
我正在使用Graph
R中的包来进行5461项的maxclique分析.
我得到的最终输出项很长,所以我收到以下警告:
到达
getOption("max.print")
- 省略了475569行
有人可以给我提供如何增加限制的指示max.print
.
我想生成一个具有base和ggplot图形组合的图形.以下代码使用R的基本绘图函数显示我的图:
t <- c(1:(24*14))
P <- 24
A <- 10
y <- A*sin(2*pi*t/P)+20
par(mfrow=c(2,2))
plot(y,type = "l",xlab = "Time (hours)",ylab = "Amplitude",main = "Time series")
acf(y,main = "Autocorrelation",xlab = "Lag (hours)", ylab = "ACF")
spectrum(y,method = "ar",main = "Spectral density function",
xlab = "Frequency (cycles per hour)",ylab = "Spectrum")
require(biwavelet)
t1 <- cbind(t, y)
wt.t1=wt(t1)
plot(wt.t1, plot.cb=FALSE, plot.phase=FALSE,main = "Continuous wavelet transform",
ylab = "Period (hours)",xlab = "Time (hours)")
Run Code Online (Sandbox Code Playgroud)
哪个生成
这些面板中的大多数看起来足以让我包含在我的报告中.但是,需要改进显示自相关的图.使用ggplot看起来好多了:
require(ggplot2)
acz <- acf(y, plot=F)
acd <- data.frame(lag=acz$lag, acf=acz$acf) …
Run Code Online (Sandbox Code Playgroud)