代码简化

时间:2017-06-24 11:19:37

标签: r list dataframe data-structures

我是R的新手,只是通过一些编码示例。有人发布了下面提到的代码。似乎他们使用dput()来发布保存数据的变量的结构。

df1 <- structure(list(keyword = c("cat", "dog", "pig", "crow", "pigeon", "hawk", "catfish", "carp"), 
                    category = c("A", "A", "A", "B", "B", "B", "C", "C")), .Names = c("Keyword", "Category"), 
                     class = "data.frame", row.names = c(NA,-8L))

df2 <- structure(list(description = structure(c(2L, 6L, 5L, 1L, 4L,3L),
                                              .Label = c("dogged dog", "false cat", "hawkward carp", "sad catfish", "shady pigeon", "smiling pig"), class = "factor")), 
                 .Names = "description", row.names = c(NA, -6L), class = "data.frame")

任何人都可以帮助我理解这些上面提到的数据结构是如何精确定义的吗?(列表,数据帧,因素?)

如何实际定义这些df1和df2,请帮助解码它们。

1 个答案:

答案 0 :(得分:0)

为了说明李哲源哲哲李的评论:

您可以使用

查看每个内容
> df1
  Keyword Category
1     cat        A
2     dog        A
3     pig        A
4    crow        B
5  pigeon        B
6    hawk        B
7 catfish        C
8    carp        C
> df2
    description
1     false cat
2   smiling pig
3  shady pigeon
4    dogged dog
5   sad catfish
6 hawkward carp

及其str

的结构
> str(df1)
'data.frame':   8 obs. of  2 variables:
 $ Keyword : chr  "cat" "dog" "pig" "crow" ...
 $ Category: chr  "A" "A" "A" "B" ...
> str(df2)
'data.frame':   6 obs. of  1 variable:
 $ description: Factor w/ 6 levels "dogged dog","false cat",..: 2 6 5 1 4 3

导致描述“它们都是data.frame,df1的列是字符串,但df2的列是因子。”

这将是重复,但您可以使用

中的class进行检查
> class(df1)
[1] "data.frame"
> class(df1$Keyword)
[1] "character"
> class(df1$Category)
[1] "character"
> class(df2)
[1] "data.frame"
> class(df2$description)
[1] "factor"