如何在R str()中展开Posixct字段?
我试图扩展一个自定义Posixct字段中显示的因子数量,其中正常方式(如何在R str()中展开Posixct字段?,r,posixct,R,Posixct,我试图扩展一个自定义Posixct字段中显示的因子数量,其中正常方式(str(DF,list.len=ncol(DF),vec.len=20))不起作用。 我在此请求20,但无论列表长度如何,它始终显示两个(“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 代码
str(DF,list.len=ncol(DF),vec.len=20)
)不起作用。
我在此请求20,但无论列表长度如何,它始终显示两个(“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
代码
R3.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)
中显示每个变量的项目数量?Etc,而不扩展变量中的参数本身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建议宽度 为了实现“更宽”的输出,您可以在R
选项中更改默认的宽度
根据选项{base}
帮助:
宽度:
控制用于打印向量、矩阵和数组的行上的最大列数,以及使用cat填充时的最大列数
它给出:
'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" ...
建议选项(宽度)
现在,使用不同的宽度
:
# 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)
罗兰宽度参数
似乎您也可以通过str
中的width
参数来实现这一点。正如罗兰建议的那样。但同样,您必须为输出提供足够大的价值。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)
总共有20个POSIXlt字符串
解释
输出问题源于为POSIXlt对象调用的utils:::str.POSIXt
方法。有趣的部分在下一行:
larg[["vec.len"]] <- min(larg[["vec.len"]], (larg[["width"]] -
nchar(larg[["indent.str"]]) - 31)%/%19)
larg[[“vec.len”]]在问题标题中编写POSIXct,然后创建一个POSIXlt变量。如果你创建了一个POSIXct变量,你可能就不会有这个问题了。我不能用R3.4.0复制。我看到了预期的结果。2.使用as.POSIXct
代替strtime
。很少有理由将时间戳存储为POSIXlt。请阅读debian on的官方说明。我在mac上再次尝试,以确保它不是win vs*nix。如果控制台的宽度足够大,它将按预期工作。尝试str(DF,list.len=ncol(DF),vec.len=20,width=100)
。将str
中的width=
参数设置为足够大的数值对我来说很有用@罗兰已经提出了这个建议,但我没有看到你的回应。您能确认这对您不起作用吗?您可以N我已经更新了正确变量声明的答案。现在,string\u size
是使用nchar
计算的,因此更容易使用不同的格式。另外,我现在想不出其他任何事情了。你能说说为什么在上次输出的末尾有…
?我认为它应该是一个完整的输出,但它不是。你能提供任何理由吗?我给你赏金是因为你试图回答这个难题。我还不知道你的建议到底有多可靠。还有一些事情需要澄清。我已经添加了一些关于如何计算输出字符串长度的解释。对不起我的英语,希望它有意义。
# 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"
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" ...
larg[["vec.len"]] <- min(larg[["vec.len"]], (larg[["width"]] -
nchar(larg[["indent.str"]]) - 31)%/%19)