Please forgive me for how basic this question must be, but I cannot, for the life of me, coerce my dataset into a data frame. I'm new to R but have worked in other languages (VBA and Matlab).
My data are pulling into R ds <- read_excel("Sample Data.xlsx")
as a list, checked with typeof(ds)
. I tried to coerce the list into a data frame using df <- as.data.frame(ds)
but that doesn't work either. The sample dataset is simple (4 variables with 5 observations each) and is stored on an Excel spreadsheet. I'm working in RStudio and the only package I have loaded is readxl.
I've asked colleagues and searched quite a bit, but it may be that my question isn't phrased properly.
Edit
In response to comments, I checked the class of both df and ds. class(df)
returns "data.frame" and class(ds)
returns "tbl_df "tbl" "data.frame
.
However, even df is still behaving as a list. typeof(df[1])
returns "list", while typeof(df[[1]])
returns "double", as it should. Functions I need to use aren't working because of this.
cor.test(df[1], df[2]) # returns Error in cor.test.default(df[1], df[2]) : 'x' must be a numeric vector
However, the code below gives me what I need.
cor.test(df[[1]], df[[2]]) # returns an r = .29, among other stats