简体   繁体   English

as_tibble只返回一个变量

[英]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.

相关问题 as_tibble()无法正常工作 - as_tibble() not working as expected as_tibble控制拼合级别 - as_tibble control level of flattening 避免在 dplyr 中重复 as_tibble() - Avoid repeating as_tibble() in dplyr 为什么as_tibble()round浮动到最接近的整数? - Why does as_tibble() round floats to the nearest integer? 如何在 R 中将 as_tibble() 格式的时间序列数据转换为 as_tsibble() 格式? - How to transform an as_tibble() format time series data into an as_tsibble() format in R? 带有Apache箭头的Sparklyr R失败,意外终止:未找到对象&#39;as_tibble&#39; - Sparklyr R with apache arrow fails, terminated unexpectedly: object 'as_tibble' not found 为什么 as_tibble(mtcars,rownames = NA) 不显示行名? 文档说它应该 - Why does as_tibble(mtcars,rownames = NA) not show the row names? The documentation says that it should 如何修复R中的“错误:&#39;名称空间:dplyr&#39;未导出对象&#39;as_tibble&#39;错误:包&#39;BLANK&#39;的延迟加载失败” - How to fix “Error : object ‘as_tibble’ is not exported by 'namespace:dplyr' ERROR: lazy loading failed for package ‘BLANK’” in R 使用purrr :: map在R中取消嵌套JSON时会丢失名称(取消列表,t,as_tibble) - Losing names when unnesting a JSON in R using purrr::map (unlist, t, as_tibble) 我可以控制 as_tibble 或 as.data.frame 如何处理 R 中的一维数据吗? - Can I control how as_tibble or as.data.frame treat one-dimensional data in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM