【发布时间】: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的class。 class(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
【问题讨论】: