关键值对的各种R数据存储选项:优点和缺点

时间:2016-05-19 07:24:26

标签: r

我常常对在实际问题中使用以下哪种数据对象感到困惑:

列表:

person_list<-list(age=25,weight=180,height=150,male=TRUE,profession="doctor")

阵列:

person_array<-c(age=25,weight=180,height=150,male=TRUE,profession="doctor")

哈希:

person_hash<-hash(age=25,weight=180,height=150,male=TRUE,profession="doctor")

数据框:

person_frame<-data.frame(attributes=c("age","weight","height","male","profession"),values=c(25,180,150,TRUE,"doctor"))

事后看来,数据框架的选择看起来很傻,但其他的呢?他们都好吗?

或者使用一种形式比另一种形式有优势/劣势吗?

e.g。由于哈希不是本机数据类型,因此R Studio不会自动完成它,因此这是一个小缺点。还有其他问题吗?

PS。数据并不大。因此,性能不是主要问题。易于编码是错误/错误等的可能性。

1 个答案:

答案 0 :(得分:1)

收集我的评论

在你的变种中(没有哈希)只有List才是好的。

1)数组 - 适用于一个类,因为所有类都转换为更宽的类(character此处)

2)Data.farme - 很好,但是当你以这种方式创建它时(使用c)它就变坏了

3)列出最灵活的一个

实施例

数据

person_array<-c(age=25,weight=180,height=150,male=TRUE,profession="doctor")
person_list<-list(age=25,weight=180,height=150,male=TRUE,profession="doctor")
person_frame<-data.frame(attributes=c("age","weight","height","male","profession"),values=c(25,180,150,TRUE,"doctor"))
person_frame_good<-data.frame(age=25,weight=180,height=150,male=TRUE,profession="doctor")

操作

> person_array$height+person_array$weight
Error in person_array$height : $ operator is invalid for atomic vectors
> person_array[["height"]]+person_array[["weight"]]
Error in person_array[["height"]] + person_array[["weight"]] : 
  non-numeric argument to binary operator
> person_frame$height+person_frame$weight
numeric(0)
> person_frame$values[person_frame$attributes=="height"]+person_frame$values[person_frame$attributes=="weight"]
[1] NA
Warning message:
In Ops.factor(person_frame$values[person_frame$attributes == "height"],  :
  ‘+’ не значимо для факторов
> person_list$height+person_list$weight
[1] 330
> person_frame_good$height+person_frame_good$weight
[1] 330

所以你可以看到只有list和df_good可以和var

相加

PS“'+'незначимодляфакторов”意思是“+不能用于因素”

加成

如果您使用的是RStudio,您只需查看数据即可 data.frame enter image description here

list不能 enter image description here

View命令在两种变体中都很好(如果数据可以双向表格显示)

相关问题