list 有没有办法查看列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32618369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to view a list
提问by YQW
When I have data.frame
objects, I can simply do View(df)
, and then I get to see the data.frame
in a nice table (even if I can't see all of the rows, I still have an idea of what variables my data contains).
当我有data.frame
对象时,我可以简单地做View(df)
,然后我可以data.frame
在一个漂亮的表中看到(即使我看不到所有的行,我仍然知道我的数据包含哪些变量)。
But when I have a list
object, the same command does not work. And when the list is large, I have no idea what the list looks like.
但是当我有一个list
对象时,相同的命令不起作用。当列表很大时,我不知道列表是什么样的。
I've tried head(mylist)
but my console simply cannot display all of the information at once. What's an efficient way to look at a large list in R?
我试过了,head(mylist)
但我的控制台无法一次显示所有信息。在 R 中查看大型列表的有效方法是什么?
采纳答案by Rentrop
回答by giraffehere
Here's a few ways to look at a list:
以下是查看列表的几种方法:
Look at one element of a list:
查看列表的一个元素:
myList[[1]]
Look at the head of one element of a list:
查看列表的一个元素的头部:
head(myList[[1]])
See the elements that are in a list neatly:
整齐地查看列表中的元素:
summary(myList)
See the structure of a list (more in depth):
查看列表的结构(更深入):
str(myList)
Alternatively, as suggested above you could make a custom print method as such:
或者,如上所述,您可以制作自定义打印方法:
printList <- function(list) {
for (item in 1:length(list)) {
print(head(list[[item]]))
}
}
The above will print out the head of each item in the list.
以上将打印出列表中每个项目的头部。
回答by MySchizoBuddy
You can also use a package called listviewer
您还可以使用名为listviewer的包
library(listviewer)
jsonedit( myList )
回答by Chris Watson
If you have a really large list, you can look at part of it using
如果你有一个非常大的列表,你可以使用它查看其中的一部分
str(myList, max=1)
回答by maycca
you can check the "head" of your dataframes using lapply
family:
您可以使用lapply
系列检查数据帧的“头部” :
lapply(yourList, head)
which will return the "heads" of you list.
这将返回您列表的“头”。
For example:
例如:
df1 <- data.frame(x = runif(3), y = runif(3))
df2 <- data.frame(x = runif(3), y = runif(3))
dfs <- list(df1, df2)
lapply(dfs, head)
Returns:
返回:
> lapply(dfs, head)
[[1]]
x y
1 0.3149013 0.8418625
2 0.8807581 0.5048528
3 0.2490966 0.2373453
[[2]]
x y
1 0.4132597 0.5762428
2 0.0303704 0.3399696
3 0.9425158 0.5465939
Instead of "head
" you can use any function related to the data.frames, i.e. names
, nrow
...
而不是“ head
”你可以使用相关的data.frames,即任何功能names
,nrow
...
回答by Mus
Seeing as you explicitly specify that you want to use View()
with a list, this is probably what you are looking for:
当您明确指定要View()
与列表一起使用时,这可能就是您要查找的内容:
View(myList[[x]])
Where x
is the number of the list element that you wish to view.
x
您要查看的列表元素的编号在哪里。
For example:
例如:
View(myList[[1]])
will show you the first element of the list in the standard View()
format that you will be used to in RStudio.
将以View()
您将在 RStudio 中使用的标准格式显示列表的第一个元素。
If you know the name of the list item you wish to view, you can do this:
如果您知道要查看的列表项的名称,则可以执行以下操作:
View(myList[["itemOne"]])
There are several other ways, but these will probably serve you best.
还有其他几种方法,但这些方法可能最适合您。
回答by user6741397
This is a simple edit of giraffehere's excellent answer.
这是 giraffehere 优秀答案的简单编辑。
For some lists it is convenient to only print the head of a subset of the nested objects, to print the name of the given slot above the output of head().
对于某些列表,只打印嵌套对象子集的头部很方便,在 head() 的输出上方打印给定槽的名称。
Arguments:
参数:
#'@param list a list object name
#'@param n an integer - the the objects within the list that you wish to print
#'@param hn an integer - the number of rows you wish head to print
USAGE: printList(mylist, n = 5, hn = 3)
用法:printList(mylist, n = 5, hn = 3)
printList <- function(list, n = length(list), hn = 6) {
for (item in 1:n) {
cat("\n", names(list[item]), ":\n")
print(head(list[[item]], hn))
}
}
For numeric lists, output may be more readable if the number of digits is limited to 3, eg:
对于数字列表,如果位数限制为 3,则输出可能更具可读性,例如:
printList <- function(list, n = length(list), hn = 6) {
for (item in 1:n) {
cat("\n", names(list[item]), ":\n")
print(head(list[[item]], hn), digits = 3)
}
}
回答by Posé - f
I had a similar problem and managed to solve it using as_tibble()
on my list (dplyr or tibble packages), then just use View()
as usual.
我遇到了类似的问题,并设法使用as_tibble()
我的列表(dplyr 或 tibble 包)解决了它,然后View()
照常使用。
回答by RobertMyles
In recent versions of RStudio, you can just use View()
(or alternatively click on the little blue arrow beside the object in the Global Environment pane).
在 RStudio 的最新版本中,您可以直接使用View()
(或者单击“全局环境”窗格中对象旁边的蓝色小箭头)。
For example, if we create a list with:
例如,如果我们创建一个列表:
test_list <- list(
iris,
mtcars
)
Then either of the above methods will show you:
然后上述任何一种方法都会告诉你:
回答by melbez
I like using as.matrix() on the list and then can use the standard View() command.
我喜欢在列表中使用 as.matrix() 然后可以使用标准的 View() 命令。