Reputation: 19
I'm very new to R and coding, and this is my first reach-out to SO
I have a data frame (brfss2013) with 491775 obs and 330 variables. I want to take three of those variables ($qlmentl2, $misdeprd & $misnowork) and create a new data frame called "level_unhappy" with just those three variables
I've tried this code in R Studio, and the result is a data frame with 491775 obs (correct) and 1 variable.
```{r create_level_unhappy, results='hide'}
level_unhappy <- as_tibble(brfss2013$qlmentl2, brfss2013$misdeprd, brfss2013$misnowork, validate = FALSE)
```
I've also tried this...
level_unhappy <- as.data.frame(brfss2013$qlmentl2, brfss2013$misdeprd, brfss2013$misnowork)
... and received...
Error in !optional : invalid argument type
In addition: Warning message:
In as.data.frame.integer(brfss2013$qlmentl2, brfss2013$misdeprd, :
'row.names' is not a character vector of length 491775 -- omitting it. Will be an error!
What am I missing?
Cheers, -eric
Upvotes: 1
Views: 398
Reputation: 1378
The first arguments of as.data.frame
must be a list of objects you wish to concatenate into a data frame, because the second argument is looking for the row.names
. So to avoid confusing R into thinking that your second data frame are the row.names
, and since you want to join vectors, put them all in a cbind()
level_unhappy <- as.data.frame(cbind(brfss2013$qlmentl2, brfss2013$misdeprd, brfss2013$misnowork))
You can also avoid this headache by using R's other base command data.frame
instead of as.data.frame
, which provides some circumstantial advantages depending on what you are trying to do. However, in this case your code would have worked as written with data.frame
:
level_unhappy <- data.frame(brfss2013$qlmentl2, brfss2013$misdeprd, brfss2013$misnowork)
data.frame
doesn't assume any argument is anything other than data to be combined unelss you explicate as much using an optional argument call like row.names = "r1"
for example.
Upvotes: 2
Reputation: 60080
as_tibble
will do different things depending on what you pass as the first argument. By doing as_tibble(brfss2013$qlmentl2, ...)
, you're passing a vector as a first argument. The as_tibble
method for vectors is not set up to accept multiple vectors, and the other vectors you pass end up getting used as row names etc. Instead, I think you want:
as_tibble(bfrss2013[, c("qlmentl2","misdeprd","misnowork")])
That way, you're passing a data frame as the first argument. as_tibble
will convert the dataframe to tibble
.
This only really applies if you specifically want to convert your data to tibble
though. Tibbles mostly work like dataframes, with a couple added features. If all you want to do is separate out those columns into a separate variable, you can do:
new_df <- bfrss2013[, c("qlmentl2","misdeprd","misnowork")]
Upvotes: 1