Avoid Truncated List Output of str Function in R (Example) | Expand Listed Variables
In this article you’ll learn how to expand listed variables using the str() function in the R programming language.
The article consists of this content:
Let’s dive right in:
Introducing Example Data
The first step is to create some data that we can use in the examples later on:
data <- as.data.frame(matrix(1:600, nrow = 5)) # Create example data frame data[ , 1:5] # Print five columns of data
Table 1 visualizes the output of the RStudio console that has been returned after running the previous R syntax and shows the structure of the first five variables of our example data.
Note that we have subsetted our data frame in the previous Table. The entire data frame contains 120 columns!
Let’s assume that we want to inspect the structure of our data frame variables using the str function. Then, we might apply the following R code:
str(data) # Apply str function # 'data.frame': 5 obs. of 120 variables: # ... # $ V94 : int 466 467 468 469 470 # $ V95 : int 471 472 473 474 475 # $ V96 : int 476 477 478 479 480 # $ V97 : int 481 482 483 484 485 # $ V98 : int 486 487 488 489 490 # $ V99 : int 491 492 493 494 495 # [list output truncated]
The output after executing the previous R code shows the structure of the first 99 variables in our data set. However, the remaining list output of the str function has been truncated.
Example: Avoid Truncation Using str() Function & list.len Argument
This example demonstrates how to display the entire list output when using the str function in R, i.e. without truncating the bottom part of the list.
To achieve this, we have to set the list.len argument within the str function to be equal to the number of columns in our data frame.
Consider the following R code and its output:
str(data, list.len = ncol(data)) # str function & list.len # 'data.frame': 5 obs. of 120 variables: # ... # $ V94 : int 466 467 468 469 470 # $ V95 : int 471 472 473 474 475 # $ V96 : int 476 477 478 479 480 # $ V97 : int 481 482 483 484 485 # $ V98 : int 486 487 488 489 490 # $ V99 : int 491 492 493 494 495 # $ V100: int 496 497 498 499 500 # $ V101: int 501 502 503 504 505 # $ V102: int 506 507 508 509 510 # $ V103: int 511 512 513 514 515 # $ V104: int 516 517 518 519 520 # $ V105: int 521 522 523 524 525 # $ V106: int 526 527 528 529 530 # $ V107: int 531 532 533 534 535 # $ V108: int 536 537 538 539 540 # $ V109: int 541 542 543 544 545 # $ V110: int 546 547 548 549 550 # $ V111: int 551 552 553 554 555 # $ V112: int 556 557 558 559 560 # $ V113: int 561 562 563 564 565 # $ V114: int 566 567 568 569 570 # $ V115: int 571 572 573 574 575 # $ V116: int 576 577 578 579 580 # $ V117: int 581 582 583 584 585 # $ V118: int 586 587 588 589 590 # $ V119: int 591 592 593 594 595 # $ V120: int 596 597 598 599 600
As you can see, this time the entire list has been returned to the RStudio console.
Video & Further Resources
In case you need more info on the content of this tutorial, I recommend watching the following video on my YouTube channel. In the video, I’m explaining the R syntax of this article in R.
The YouTube video will be added soon.
Furthermore, you might read the other tutorials on this website:
To summarize: In this R tutorial you have learned how to avoid truncation in the str function. In case you have any further questions, please let me know in the comments section below.
Statistics Globe Newsletter