【问题标题】:Data frame reverting to "list" in R数据框在 R 中恢复为“列表”
【发布时间】:2020-06-16 19:33:56
【问题描述】:

请原谅我这个问题是多么的基本,但我不能将我的数据集强制转换为数据框。我是 R 新手,但使用过其他语言(VBA 和 Matlab)。

我的数据被拉入 R ds <- read_excel("Sample Data.xlsx") 作为一个列表,用 typeof(ds) 检查。我尝试使用df <- as.data.frame(ds) 将列表强制转换为数据框,但这也不起作用。样本数据集很简单(4 个变量,每个变量有 5 个观察值)并存储在 Excel 电子表格中。我在 RStudio 工作,我加载的唯一包是 readxl。

我问过同事并搜索了很多,但可能是我的问题措辞不正确。

编辑 针对cmets,我检查了df和ds的classclass(df) 返回“data.frame”,class(ds) 返回 "tbl_df "tbl" "data.frame

但是,即使 df 仍然表现得像一个列表。 typeof(df[1]) 返回“list”,而 typeof(df[[1]]) 返回“double”,这是应该的。因此,我需要使用的功能无法正常工作。

cor.test(df[1], df[2]) # returns Error in cor.test.default(df[1], df[2]) : 'x' must be a numeric vector

但是,下面的代码可以满足我的需要。

cor.test(df[[1]], df[[2]]) # returns an r = .29, among other stats

【问题讨论】:

  • class "data.frame" 的对象属于 typeof "list"。试试class(ds)
  • read_excel 来自readxl 包,默认返回tibble,如果您拉入多张纸,您将获得一个列表。默认情况下,它应该只返回第一个。你可以试试sheet = 1。如果做不到这一点,请将dput(ds) 的一些输出作为edit 提供给您的问题。
  • 还有relevant
  • 感谢您的参考,@RuiBarradas
  • ...既然我们讨论的是提取运算符的主题,this 也很重要。

标签: r dataframe


【解决方案1】:

我认为你得到了正确的数据框。来自包readxl 的函数read_excel() 应该返回一个tibble,它是一种特殊类型的数据帧。 (如果你不提供工作表名称,它只需要第一个工作表并返回一个小标题。)

Tibble 是 list 类型,类似于数据框。检查内置数据框mtcars

typeof(mtcars)

要获取对象的类,请输入class(ds),您会看到它是一个数据框和一个小标题。所以你应该可以像使用数据框一样使用它,不用担心。

要引用它的行或列,只需键入df[rows, columns],以便根据您的情况:

cor.test(df[ ,1], df[ ,2])

【讨论】:

  • 感谢您的评论!但是,请参阅我的编辑。我只是无法让它按应有的方式工作。
  • @KyleS 是的,你可能想引用列,所以输入cor.test(df[ ,1], df[ ,2]),因为方括号中的第一个(省略)数字是行,第二个是列。
  • 谢谢你,Petr,这正是问题所在。发送给我的一位同事的代码有数据集[1],并且在他的计算机上运行良好。不太确定他做了什么,但这很好用。感谢您的耐心等待!
【解决方案2】:

问题中列出的问题是由于提取运算符的[[[ 形式的行为差异造成的。

提取运算符的[ 形式在用于数据帧时会返回另一个数据帧,该数据帧也是一个列表。

str(mtcars[1])
'data.frame':   32 obs. of  1 variable:
 $ mpg: num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...

提取运算符的[[ 形式返回一个向量。

str(mtcars[[1]])
 num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...

由于base::cor.test() 将向量作为输入,因此必须使用提取运算符的[[ 形式、[ 运算符的data frame[,col] 版本或$ 形式。例如:

cor.test(mtcars[,1],mtcars[,4])
cor.test(mtcars[[1]],mtcars[[4]])
cor.test(mtcars$mpg,mtcars$hp)

...所有这些都返回相同的结果:

> cor.test(mtcars$mpg,mtcars$hp)

    Pearson's product-moment correlation

data:  mtcars$mpg and mtcars$hp
t = -6.7424, df = 30, p-value = 1.788e-07
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 -0.8852686 -0.5860994
sample estimates:
       cor 
-0.7761684

注意:一些 R 函数可以处理数据帧而不是向量的输入,例如 psych::corr.test()

> psych::corr.test(mtcars[1],mtcars[4])
Call:psych::corr.test(x = mtcars[1], y = mtcars[4])
Correlation matrix 
       hp
mpg -0.78
Sample Size 
[1] 32
Probability values  adjusted for multiple tests. 
    hp
mpg  0

 To see confidence intervals of the correlations, print with the short=FALSE option

【讨论】:

  • 非常感谢您的回答。我需要更多地了解 R 的来龙去脉,所以这也有帮助。
  • @KyleS - 不客气。提取运算符是 R 中极其重要的组成部分,许多 R 初学者都在为它而苦恼。有关此主题的更多信息,您可以查看Forms of the Extract Operator,这是我为支持 Coursera 上的约翰霍普金斯大学 R 编程课程而写的。
猜你喜欢
  • 2016-09-10
  • 1970-01-01
  • 2016-07-24
  • 2017-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多