我正在尝试扩展一个自定义Posixct字段中显示的因素的数量,而正常方式(str(DF, list.len=ncol(DF), vec.len=20))不起作用。我在这里请求20,但它始终显示2 ("2017-01-01 08:40:00" "2017-01-01 08:50:00" ...),而不考虑列表的长度(这里是3)。数据data.csv
"AAA", "BBB"
1, 01012017-0940+0100
2, 01012017-0950+0100
3, 01012017-0838+0100代码
library('methods') # setClass
# https://unix.stackexchange.com/a/363290/16920
setClass('iso8601')
# https://stackoverflow.com/questions/5788117/only-read-limited-number-of-columns
setAs("character","iso8601",function(from) strptime(from,format="%d%m%Y-%H%M%z"))
DF <- read.csv(file='data.csv',
sep=',',
header=TRUE,
colClasses=c('numeric','iso8601'),
strip.white=TRUE)
DF
str(DF, list.len=ncol(DF), vec.len=20)R 3.3.3的产出
AAA BBB
1 1 2017-01-01 08:40:00
2 2 2017-01-01 08:50:00
3 3 2017-01-01 07:38:00
'data.frame': 3 obs. of 2 variables:
$ AAA : num 1 2 3
$ BBB : POSIXlt, format: "2017-01-01 08:40:00" "2017-01-01 08:50:00" ...R 3.4.0的产出
和上面一样,复制相同的问题。
AAA BBB
1 1 2017-01-01 08:40:00
2 2 2017-01-01 08:50:00
3 3 2017-01-01 07:38:00
'data.frame': 3 obs. of 2 variables:
$ AAA: num 1 2 3
$ BBB: POSIXlt, format: "2017-01-01 08:40:00" "2017-01-01 08:50:00" ...str(DF, list.len=ncol(DF), vec.len=20)扩展到每个变量的许多因素?str(DF)中显示每个变量的项数?等不展开参数本身在变量中。消除病因学中的终末宽度和列因子
我做到了
Rscript myScript.r罗兰的建议
代码不是在所有情况下都起作用,而是在有限的情况下工作,因此应该可以动态地应用它。
# Roland's comment
str(DF, list.len=ncol(DF), vec.len=20, width = 100)R: 3.3.3,3.4.0 (2017-04-21,后台)
操作系统: Debian 8.7
窗口管理器: Gnome 3.14.1
发布于 2017-05-27 07:48:28
建议宽度
为了实现“更宽”的输出,您可以在R options中更改默认的options。
根据options {base}帮助:
宽度: 控制在打印向量、矩阵和数组时以及在cat填充时使用的行上的最大列数。
Here is an example:
# initial try
str(DF, list.len=ncol(DF), vec.len=20)它规定:
'data.frame': 3 obs. of 2 variables:
$ AAA: num 1 2 3
$ BBB: POSIXlt, format: "2017-01-01 11:40:00" "2017-01-01 11:50:00" ...提案选项(宽度)
现在,使用不同的width
# retain default options
op <- options()
# set apropriate width
n_cols <- 22 * 20 # n columns for 20 POSIXlt strings
n_cols <- n_cols + 50 # 50 columns for column description
# actually you can use any sufficiently big number
# for example n_cols = 1000
options(width = n_cols)
str(DF, list.len=ncol(DF), vec.len=20)
options(op)结果是:
'data.frame': 3 obs. of 2 variables:
$ AAA: num 1 2 3
$ BBB: POSIXlt, format: "2017-01-01 11:40:00" "2017-01-01 11:50:00" "2017-01-01 10:38:00"罗兰宽度参数
似乎您也可以使用width参数在str中实现这一点。就像罗兰建议的那样。但同样,你必须为产出提供足够大的价值。1 POSIXlt字符串包含21个字符+空格。因此,对于20个字符串,需要超过440列。
三参数法
我试过用你的例子:
DF <- rbind(DF, DF, DF) # nrows = 24
# Calculate string width
string_size <- nchar(as.character(DF[1, 2])) + 3 # string width + "" and \w
N <- 20 # number of items
n_cols <- string_size * N
str(DF, list.len=ncol(DF), vec.len=20, width = n_cols)输出:
'data.frame': 24 obs. of 2 variables:
$ AAA: num 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3
$ BBB: POSIXlt, format: "2017-01-01 11:40:00" "2017-01-01 11:50:00" "2017-01-01 10:38:00" "2017-01-01 11:40:00" "2017-01-01 11:50:00" "2017-01-01 10:38:00" "2017-01-01 11:40:00" "2017-01-01 11:50:00" "2017-01-01 10:38:00" "2017-01-01 11:40:00" "2017-01-01 11:50:00" "2017-01-01 10:38:00" "2017-01-01 11:40:00" "2017-01-01 11:50:00" "2017-01-01 10:38:00" "2017-01-01 11:40:00" "2017-01-01 11:50:00" "2017-01-01 10:38:00" "2017-01-01 11:40:00" "2017-01-01 11:50:00" ...确切地说有20个POSIXlt字符串。
解释
输出问题源于utils:::str.POSIXt方法,该方法是为POSIXlt对象调用的。有趣的部分在下一行:
larg[["vec.len"]] <- min(larg[["vec.len"]], (larg[["width"]] -
nchar(larg[["indent.str"]]) - 31)%/%19)这一行计算输出中POSIXlt字符串的数量。粗略地说,输出将不超过vec.len POSIXlt字符串,以字符表示的输出长度将不超过width。
在这里,larg是传递给str的参数列表。默认情况下,它们是:vec.len = 4;width = 80;indent.str = " "。
因此,在默认情况下,重新计算的vec.len为2。
对于最后一个例子,我们设置了vec.len = 20、width = 440,并且我们的数据帧有24行。重新计算的vec.length为20。因此输出str(DF)包含20个POSIXlt字符串,并以‘.’尾结尾,这意味着POSIXlt向量中有超过20个元素。
https://stackoverflow.com/questions/44026585
复制相似问题