How to expand Posixct field in R str()?

血红的双手。 提交于 2019-12-03 11:14:15

Proposal width

In order to achieve "wider" output, you can change default width in R options.

According to options {base} help:

width:

controls the maximum number of columns on a line used in printing vectors, matrices and arrays, and when filling by cat.

Here is an example:
# initial try
str(DF, list.len=ncol(DF), vec.len=20)

it gives:

    '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" ...

Proposal options(width)

And now, with different 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)

The result is:

'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"

Roland's width parameter

It seems like you can achieve this as well with width parameter in str. Just as Roland suggested. But again you have to provide big enough value for output. 1 POSIXlt string contains 21 characters + whitespace. So for 20 strings, you need more than 440 columns.

Three parameter approach

I have tried it with your example:

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)

Output:

'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" ...

There are exactly 20 POSIXlt strings.

Explanation

The problem with output arises from utils:::str.POSIXt method which is called for POSIXlt object. The interesting part is in next line:

larg[["vec.len"]] <- min(larg[["vec.len"]], (larg[["width"]] - 
                nchar(larg[["indent.str"]]) - 31)%/%19)

This line computes the number of POSIXlt strings in output. Roughly saying output will consist of NOT more than vec.len POSIXlt strings AND the length of output in characters will be NOT more than width.

Here, larg is a list of arguments passed to str. By default they are: vec.len = 4; width = 80; indent.str = " ".

So, the recomputed vec.len by default will be 2.

As to last example, we set vec.len = 20, width = 440 and our data frame has 24 rows. Recomputed vec.length is 20. So the output str(DF) contains 20 POSIXlt strings and tailed with '...', which means that there are more than 20 elements in the POSIXlt vector.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!