[英]as_tibble only returns a single variable
I'm very new to R and coding, and this is my first reach-out to SO 我对R和编码很新,这是我第一次接触到SO
I have a data frame (brfss2013) with 491775 obs and 330 variables. 我有一个数据框(brfss2013),491775个obs和330个变量。 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 我想取三个变量($ qlmentl2,$ misdeprd和$ misnowork)并用这三个变量创建一个名为“level_unhappy”的新数据框
I've tried this code in R Studio, and the result is a data frame with 491775 obs (correct) and 1 variable. 我在R Studio中尝试了这个代码,结果是一个带有491775个obs(正确)和1个变量的数据框。
```{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 干杯,讽刺
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
. as.data.frame
的第一个参数必须是您希望连接到数据框的对象列表,因为第二个参数是查找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()
因此,为了避免让R混淆认为您的第二个数据框是row.names
,并且因为您想要连接向量 ,请将它们全部放在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. 您还可以使用R的其他基本命令data.frame
而不是as.data.frame
来避免这种麻烦,这取决于您尝试执行的操作,这提供了一些间接优势。 However, in this case your code would have worked as written with data.frame
: 但是,在这种情况下,您的代码将使用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. data.frame
不假设任何参数是除了要组合的数据之外的其他任何东西,你可以使用像row.names = "r1"
这样的可选参数调用来解释。
as_tibble
will do different things depending on what you pass as the first argument. as_tibble
将根据您传递的内容作为第一个参数执行不同的操作。 By doing as_tibble(brfss2013$qlmentl2, ...)
, you're passing a vector as a first argument. 通过执行as_tibble(brfss2013$qlmentl2, ...)
,您将传递一个向量作为第一个参数。 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
方法未设置为接受多个向量,而您传递的其他向量最终会被用作行名称等。相反,我认为您需要:
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
. as_tibble
将转换数据帧到tibble
。
This only really applies if you specifically want to convert your data to tibble
though. 这只适用于您特别希望将数据转换为tibble
。 Tibbles mostly work like dataframes, with a couple added features. Tibbles主要像数据帧一样工作,增加了一些功能。 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")]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.