1

我是新来的R。我们使用 Trip Advisor 的数据集来获取每个指定数组中的“Value”、“OverAll”、“Rooms”等值。结果如下

Value <- as.numeric(c("4","3","5","2.5"))

Overall <- as.numeric(c("4","4.5","2","3"))

Rooms <- as.numeric(c("3","4","2","2"))

我想在 Table 中获取值。

Value    4    3     5     2.5

Overall    4    4.5    2    3

Rooms    3    4     2     2

我试图将其转换为 2D 数组以及table(Value,Overall,Rooms). 转换为数组会导致错误,并且table()没有将其更改为我需要的格式。请指导我哪里错了。

4

3 回答 3

3

采用rbind

rbind(Value = c("4","3","5","2.5"),
      Overall = c("4","4.5","2","3"),
      Rooms = c("3","4","2","2"))

        [,1] [,2]  [,3] [,4] 
Value   "4"  "3"   "5"  "2.5"
Overall "4"  "4.5" "2"  "3"  
Rooms   "3"  "4"   "2"  "2"  

由于您在此处操作分数(TripAdvisor 注释),因此最好在创建矩阵之前将它们转换为数字:

rbind(Value = as.numeric(c("4","3","5","2.5")),
      Overall = as.numeric(c("4","4.5","2","3")),
      Rooms =as.numeric( c("3","4","2","2")))

       [,1] [,2] [,3] [,4]
Value      4  3.0    5  2.5
Overall    4  4.5    2  3.0
Rooms      3  4.0    2  2.0
于 2013-03-13T14:17:30.170 回答
2

如果你想要数据框:

data.frame(Value=Value,Overall=Overall,Rooms=Rooms)

或矩阵:

 rbind(Value=Value,Overall=Overall,Rooms=Rooms)

请注意,由于您已通过

Value <- c("4","3","5","2.5")
Overall <- c("4","4.5","2","3")
Rooms <- c("3","4","2","2")

代替

Value <- c(4,3,5,2.5)
Overall <- c(4,4.5,2,3)
Rooms <- c(3,4,2,2)

你得到字符而不是数字。现在,如果您进行一些建模,值“4”、“3”等被视为因素(请参阅 参考资料?factors),因此您不能拥有诸如 3.5 之类的值,我认为您希望拥有它,因为您也拥有 2.5 等。

该函数table用于计数表和类似的东西:

table(c("a","b","a","d","abc","b","b"))
  a abc   b   d 
  2   1   3   1 
于 2013-03-13T14:18:30.723 回答
1

我建议您收集 JSON 数据集,然后您可以轻松解析它们,因为 R 非常适合从 JSON 中提取数据。您可以访问:https ://www.rstudio.com/online-learning/

于 2017-08-05T18:08:02.853 回答