以下一致性导致我的R会话崩溃.
在两台机器上测试,Ubuntu和Mac OS X两者都有类似的结果.
简要说明:使用所有NA的因子列
调用write.tabledata.frame.
原始数据集相当大,我设法隔离了有问题的列,然后创建了一个类似的向量,命名PROBLEM_DATA如下,导致相同的崩溃.
有趣的是,有时R崩溃是彻头彻尾的,它只会引发以下错误:
Error in write.table(x, file, nrow(x), p, rnames, sep, eol, na, dec, as.integer(quote), :
'getCharCE' must be called on a CHARSXP
Run Code Online (Sandbox Code Playgroud)
违规数据和电话:
PROBLEM_DATA <- structure(114:116, .Label = c("String1", "String2", "String3", "String4", "String5", "String6",
"String7", "String8", "String9", "String10", "String11", "String12", "String13", "String14", "String15"), class = "factor")
# This will cause a crash
write.table(PROBLEM_DATA, file=path.expand("~/test.csv"))
# This will also crash
write.table(PROBLEM_DATA, file=path.expand("~/test.csv"), fileEncoding="UTF-8")
Run Code Online (Sandbox Code Playgroud)
R version 2.15.3 …Run Code Online (Sandbox Code Playgroud) 更新:
该文艺青年最爱的是,RJSONIO不再是两个选项就越快.相反rjson,现在要快得多.
请参阅注释以获得更多结果确认
我的印象RJSONIO应该是更快rjson.
但是,我得到了相反的结果.
我的问题是:
RJSONIO?(也就是说,我忽略了什么?)下面是使用真实数据的比较(Ujson网页的内容在哪里),然后是模拟的json
## REAL DATA
library(microbenchmark)
> microbenchmark(RJSONIO::fromJSON(U), rjson::fromJSON(U))
Unit: milliseconds
expr min lq median uq max
1 rjson::fromJSON(U) 29.46913 30.16218 31.74999 34.11012 158.6932
2 RJSONIO::fromJSON(U) 175.11514 181.67742 186.52871 195.90646 414.6160
> microbenchmark(RJSONIO::fromJSON(U, simplify=FALSE), rjson::fromJSON(U))
Unit: milliseconds
expr min lq median uq max
1 rjson::fromJSON(U) 27.92341 28.7430 29.60091 30.63291 1 143.9478
2 RJSONIO::fromJSON(U, simplify = FALSE) 173.30136 179.5815 183.94315 190.17245 …Run Code Online (Sandbox Code Playgroud) 我想知道,如果给定一个命名向量,是否可以打印(或在R控制台中显示)只有矢量的值而不删除名称.
# EXAMPLE
v <- (1:5)
names(v) <- LETTERS[1:5]
print(v)
# RESULT:
# A B C D E
# 1 2 3 4 5
# RESULT I AM SEEKING
# [1] 1 2 3 4 5
Run Code Online (Sandbox Code Playgroud)
我能够使用以下函数获得我正在寻找的结果.但是,是否有更好或更直接的方法只打印命名向量的值?
print.n <- function (obj) {
names(obj) <- NULL
print(obj)
}
print.n(v)
# [1] 1 2 3 4 5
Run Code Online (Sandbox Code Playgroud)
谢谢.
在非常频繁调用的函数内包含library/ require语句是否有任何不利影响?
使用的时间似乎相当可以忽略不计,但我每隔几分钟调用一次这个功能,我想知道重复require呼叫是否有任何缺点?
请注意,该功能只是一个个人工具,并没有被共享.即,我是唯一使用它的人
顺便说一下,任何关于为什么library一半的见解都慢了require?我的印象是他们是同义词.
WithREQUIRE <- function(x) {
require(stringr)
str_detect(x, "hello")
}
WithLIBRARY <- function(x) {
library(stringr)
str_detect(x, "hello")
}
Without <- function(x) {
str_detect(x, "hello")
}
x <- "goodbye"
library(rbenchmark)
benchmark(WithREQUIRE(x), WithLIBRARY(X), Without(x), replications=1e3, order="relative")
# test replications elapsed relative user.self sys.self
# Without(x) 1000 0.592 1.000 0.262 0.006
# WithREQUIRE(x) 1000 0.650 1.098 0.295 0.015
# WithLIBRARY(X) 1000 1.359 2.296 0.572 0.024
Run Code Online (Sandbox Code Playgroud) 好吧,这个有点奇怪......似乎通过使用:=运算符在data.table中创建一个新列,以前分配的变量(使用colnames创建)会静默更改.
这是预期的行为吗?如果不是有什么问题?
# Lets make a simple data table
require(data.table)
dt <- data.table(fruit=c("apple","banana","cherry"),quantity=c(5,8,23))
dt
fruit quantity
1: apple 5
2: banana 8
3: cherry 23
# and assign the column names to a variable
colsdt <- colnames(dt)
str(colsdt)
chr [1:2] "fruit" "quantity"
# Now let's add a column to the data table using the := operator
dt[,double_quantity:=quantity*2]
dt
fruit quantity double_quantity
1: apple 5 10
2: banana 8 16
3: cherry 23 46
# ... and WITHOUT explicitly changing 'colsdt', …Run Code Online (Sandbox Code Playgroud) 在下面的示例中,当名称设置为NULL,all.equalthrows 'Error: not compatible with STRSXP'
但是,如果将名称设置为NA(或某个其他值),则all.equal正常工作.
这是预期的行为还是一个错误?
## SAMPLE DATA
set.seed(1)
x <- data.frame(LETTERS[1:3], rnorm(3))
names(x) <- NULL
x
# NA NA
# 1 A -0.626454
# 2 B 0.183643
# 3 C -0.835629
all.equal(x, x)
# Error: not compatible with STRSXP
# add names back in, even 'NA'
names(x) <- c(NA, NA)
all.equal(x, x)
# [1] TRUE
Run Code Online (Sandbox Code Playgroud) 从矩阵中的每列中提取min的最快方法是什么?
将所有基准移至下面的答案.
## TEST DATA
set.seed(1)
matrix.inputs <- list(
"Square Matrix" = matrix(sample(seq(1e6), 4^2*1e4, T), ncol=400), # 400 x 400
"Tall Matrix" = matrix(sample(seq(1e6), 4^2*1e4, T), nrow=4000), # 4000 x 40
"Wide-short Matrix" = matrix(sample(seq(1e6), 4^2*1e4, T), ncol=4000), # 40 x 4000
"Wide-tall Matrix" = matrix(sample(seq(1e6), 4^2*1e5, T), ncol=4000), # 400 x 4000
"Tiny Sq Matrix" = matrix(sample(seq(1e6), 4^2*1e2, T), ncol=40) # 40 x 40
)
Run Code Online (Sandbox Code Playgroud) 给定一个字符串向量,我想创建一个没有引号的表达式.
# eg, I would like to go from
c("string1", "string2")
# to... (notice the lack of '"' marks)
quote(list(string1, string2))
Run Code Online (Sandbox Code Playgroud)
我在删除引号时遇到了一些困难
input <- c("string1", "string2")
output <- paste0("quote(list(", paste(input, collapse=","), "))")
# not quite what I am looking for.
as.expression(output)
expression("quote(list(string1,string2))")
Run Code Online (Sandbox Code Playgroud)
library(data.table)
mydt <- data.table(id=1:3, string1=LETTERS[1:3], string2=letters[1:3])
result <- ????? # some.function.of(input)
> mydt[ , eval( result )]
string1 string2
1: A a
2: B b
3: C c
Run Code Online (Sandbox Code Playgroud) 我正在xls使用导入文件gdata.我正在使用转换日期列as.Date来转换日期
根据手册as.Date,日期来源取决于平台,因此我决定相应使用哪个来源
.origin <- ifelse(Sys.info()[['sysname']] == "Windows", "1899-12-30", "1904-01-01")
as.Date(myData$Date, origin=.origin)
Run Code Online (Sandbox Code Playgroud)
但是,我想知道我是否应该考虑正在读取文件的平台或编写文件的平台?
为了它的价值,我目前正在测试没有excel的linux盒子上的代码,并且使用正确的Dates生成 origin="1904-01-01"
引用`?as.Date'
## date given as number of days since 1900-01-01 (a date in 1989)
as.Date(32768, origin = "1900-01-01")
## Excel is said to use 1900-01-01 as day 1 (Windows default) or
## 1904-01-01 as day 0 (Mac default), but this is complicated by Excel
## treating 1900 as a leap year.
## So for …Run Code Online (Sandbox Code Playgroud) f <- function(PLOT, TITLE) {
PLOT + ggtitle(TITLE)
}
Run Code Online (Sandbox Code Playgroud)
直接调用该函数可以按预期工作.
但是,作为对象do.call(f, ..)时,通过调用函数会抛出错误TITLElanguage
## Sample Data
TIT <- bquote(atop("This is some text", atop(italic("Here is some more text"))))
P <- qplot(x=1:10, y=1:10, geom="point")
## WORKS FINE
f(P, TIT)
## FAILS
do.call(f, list(P, TIT))
## Error in labs(title = label) : could not find function "atop"
Run Code Online (Sandbox Code Playgroud)
这当然只有在TIT语言对象时才会发生
TIT.char <- "This is some text\nHere is some more text"
do.call(f, list(P, TIT.char))
## No Error
Run Code Online (Sandbox Code Playgroud)
do.call()当参数是语言对象时如何正确使用?
r ×10
data.table ×2
benchmarking ×1
crash ×1
date ×1
do.call ×1
dplyr ×1
excel ×1
expression ×1
gdata ×1
ggplot2 ×1
matrix ×1
min ×1
quote ×1
require ×1
rjson ×1
rjsonio ×1
write.table ×1
xls ×1