tibble

Row to colnames

守給你的承諾、 提交于 2020-02-22 05:57:02
问题 Is there a quick way (part of the tidyverse API perhaps) to turn a row into column names for a data.frame or tibble , somewhat similar to tibble::column_to_rownames ? I realize there are many ways to do this, e.g. somewhat clumsily: > df <- head(iris) > > df %>% + set_colnames(magrittr::extract(., 1,)) %>% + magrittr::extract(-1,) 5.1 3.5 1.4 0.2 1 2 4.9 3.0 1.4 0.2 setosa 3 4.7 3.2 1.3 0.2 setosa 4 4.6 3.1 1.5 0.2 setosa 5 5.0 3.6 1.4 0.2 setosa 6 5.4 3.9 1.7 0.4 setosa 回答1: janitor::row_to

Row to colnames

青春壹個敷衍的年華 提交于 2020-02-22 05:56:53
问题 Is there a quick way (part of the tidyverse API perhaps) to turn a row into column names for a data.frame or tibble , somewhat similar to tibble::column_to_rownames ? I realize there are many ways to do this, e.g. somewhat clumsily: > df <- head(iris) > > df %>% + set_colnames(magrittr::extract(., 1,)) %>% + magrittr::extract(-1,) 5.1 3.5 1.4 0.2 1 2 4.9 3.0 1.4 0.2 setosa 3 4.7 3.2 1.3 0.2 setosa 4 4.6 3.1 1.5 0.2 setosa 5 5.0 3.6 1.4 0.2 setosa 6 5.4 3.9 1.7 0.4 setosa 回答1: janitor::row_to

Preserve original type `tibble` in nested columns

Deadly 提交于 2020-01-24 14:05:15
问题 I really like the new tidyr interface that came with v1.0.0 . However, with the tidyverse more or less being centered around tibble , I was a bit puzzled that the nested column seems to be a list of data.frame s - even when the original data was a tibble to begin with (in which case I would have expected that I end up with a list of tibble s in the nested column): library(magrittr) df <- tibble::tribble( ~id, ~x, ~y, 1, 10, 20, 1, 100, 200, 2, 1, 2 ) df #> # A tibble: 3 x 3 #> id x y #> <dbl>

How to apply a “complicated” user defined function on each element of a tibble

自古美人都是妖i 提交于 2020-01-17 06:13:08
问题 I have searched high and low for the answer to this (seemingly simple) problem, but came up empty so I hope someone can help me or point me in the right direction. I have a fairly complicated submodel that I want to apply to a dataset, but if I just use use mutate I get an error Variables must be length 1 or 21. adding rowwise() doesnt seem to impact it. Let me use the following silly illustration of the problem: myData <- tibble(x=10:20, y=c("a", "b","a", "b","a", "b","a", "b","a", "b","a"))

In the tidyverse, what is the difference between an object of class “tbl” and “tbl_df”?

老子叫甜甜 提交于 2020-01-15 11:24:12
问题 When creating a tibble, tbl <- tibble(A=1:5, B=6:10) the result of class(tbl) is [1] "tbl_df" "tbl" "data.frame" I'm used to seeing this as I use dplyr quite a bit. But when is an object just a "tbl" (and not a "tbl_df") or vice versa? I'd just like to know a bit more about the difference, if any. Any documentation would be much appreciated! 回答1: You can think of a "tibble" as an interface. If an object can respond to all the tibble actions, then you can think of it as a tibble. R doesn't

How to find the longest duplicate sequence in a tibble column?

天大地大妈咪最大 提交于 2019-12-31 02:41:45
问题 I updated my question because I need one more column to my output tible. I have the following tibble: library(tibble) my_tbl <- tribble( ~year, ~event_id, ~winner_id, 2011, "A", 4322, 2012, "A", 4322, 2013, "A", 4322, 2014, "A", 5478, 2015, "A", 4322, 2011, "B", 4322, 2012, "B", 7893, 2013, "B", 7893, 2014, "B", 2365, 2015, "B", 3407, 2011, "C", 5556, 2012, "C", 5556, 2013, "C", 1238, 2014, "C", 2391, 2015, "C", 2391, 2011, "D", 4219, 2012, "D", 7623, 2013, "D", 8003, 2014, "D", 2851, 2015,

How to find the longest duplicate sequence in a tibble column (follow up question)?

烈酒焚心 提交于 2019-12-24 22:32:09
问题 This is a follow up question to the one I posted here: How to find the longest duplicate sequence in a tibble column? As the output, I need one more column (years): library(tibble) library(purrr) my_tbl <- tribble( ~year, ~event_id, ~winner_id, 2011, "A", 4322, 2012, "A", 4322, 2013, "A", 4322, 2014, "A", 5478, 2015, "A", 4322, 2011, "B", 4322, 2012, "B", 7893, 2013, "B", 7893, 2014, "B", 2365, 2015, "B", 3407, 2011, "C", 5556, 2012, "C", 5556, 2013, "C", 1238, 2014, "C", 2391, 2015, "C",

Row subsets of `Tibble` loses custom s3 class

我只是一个虾纸丫 提交于 2019-12-24 19:29:45
问题 If I extract a row from a dataframe, my custom s3 class stays: test_df = iris class(test_df) <- c("test_class", class(test_df)) class(test_df[1,]) [1] "test_class" "data.frame" But this does not work for tibbles: test_df <- as_tibble(test_df) class(test_df) <- c("test_class", class(test_df)) class(test_df[1,]) [1] "tbl_df" "tbl" "data.frame" Is there a way around this? Thanks 回答1: The answer comes from the s3 section of Hadley's Advanced R book. You have to define a class constructor function

Time zone gets lost with lubridate when creating a new datetime component from it

我的未来我决定 提交于 2019-12-24 03:55:34
问题 The following data frame is from a dput . I have used forced_tz on the datatime with no argument. I live in UTC+1 time zone. library(lubridate) library(dplyr) df <- structure(list(value = structure(c(1514967058, 1515148132, 1517472989, 1543844646, 1525085884, 1520584330, 1522838681, 1540379051, 1516707360, 1516705706), class = c("POSIXct", "POSIXt"))), .Names = "value", row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame")) tz(df$value) [1] "" df2 <- df %>% mutate(year=year(value)

R: Unwanted quotation marks in tibble::lst names

本秂侑毒 提交于 2019-12-24 01:50:18
问题 I only recently learned of tibble::lst , which creates a list object but automatically names list items. I'm using this as a shortcut within a %>% workflow that makes use of the names as the .id argument in map_dfr , so the automatic naming is really helpful. However, the names are coming in with quotation marks around them. I noticed this because they awkwardly printed in axis tick labels in a ggplot , i.e. I had a label saying "Hartford" instead of Hartford . I looked through issues on the