tidyr

Transforming a nested data frame with varying number of elements

|▌冷眼眸甩不掉的悲伤 提交于 2020-02-02 04:38:03
问题 I have a data frame with a column of nested data frames with 1 or 2 columns and n rows. It looks like df in the sample below: 'data.frame': 3 obs. of 2 variables: $ vector:List of 3 ..$ : chr "p1" ..$ : chr "p2" ..$ : chr "p3" $ lists :List of 3 ..$ :'data.frame': 2 obs. of 2 variables: .. ..$ n1: Factor w/ 2 levels "a","b": 1 2 .. ..$ n2: Factor w/ 2 levels "1","2": 1 2 ..$ :'data.frame': 1 obs. of 1 variable: .. ..$ n1: Factor w/ 1 level "d": 1 ..$ :'data.frame': 1 obs. of 2 variables: .. .

tidyverse - prefered way to turn a named vector into a data.frame/tibble

走远了吗. 提交于 2020-01-31 22:57:45
问题 Using the tidyverse a lot i often face the challenge of turning named vectors into a data.frame / tibble with the columns being the names of the vector. What is the prefered/tidyversey way of doing this? EDIT: This is related to: this and this github-issue So i want: require(tidyverse) vec <- c("a" = 1, "b" = 2) to become this: # A tibble: 1 × 2 a b <dbl> <dbl> 1 1 2 I can do this via e.g.: vec %>% enframe %>% spread(name, value) vec %>% t %>% as_tibble Usecase example: require(tidyverse)

Turn a list of lists with unnamed entries into a data frame or a tibble

我的未来我决定 提交于 2020-01-30 12:13:44
问题 I'm using the reticulate R package from RStudio to run some python code to bring data from ROOT (http://root.cern.ch) into R. My problem is that the python code returns a list of row-wise lists. For example, in python, [[0L, 0L, 'mu+', 1, 0, 0, 1, 3231.6421853545253, -17.361063509909364, 6322.884067996471, -2751.857298366544, 1.2318766603937736, 1407.9560948453036, 3092.931322317615], [0L, 0L, 'nu_e', 3, 1, 0, 0, 3231.6421853545253, -17.361063509909364, 6322.884067996471, -743.6755000649275,

spread for duplicate identifiers [duplicate]

Deadly 提交于 2020-01-30 11:34:11
问题 This question already has answers here : How to reshape data from long to wide format (11 answers) Closed 2 years ago . I'm really sorry to ask this question again, because there are already many questions about this. However, none of the solutions worked for my problem. My data looks like this: id scale rater rating 1 A 1 5 1 B 1 7 1 A 2 3 1 B 2 6 2 A 1 4 2 B 1 3 2 A 2 2 2 B 2 1 I want to spread(rater, rating) In the end it should look like this: id scale 1 2 1 A 5 3 1 B 7 6 2 A 4 2 2 B 3 1

Spread with duplicate identifiers for rows [duplicate]

拈花ヽ惹草 提交于 2020-01-28 09:22:05
问题 This question already has answers here : Using spread with duplicate identifiers for rows (4 answers) Closed 2 years ago . There has been questions on this topic before here, but I am still struggling with spreading this. I would like so each state to have its own column of temperatures values. Here is a dput() of my data. I'll call it df structure(list(date = c("2018-01-21", "2018-01-21", "2018-01-20", "2018-01-20", "2018-01-19", "2018-01-19", "2018-01-18", "2018-01-18", "2018-01-17", "2018

Creating a “crosstabs” style output

泄露秘密 提交于 2020-01-25 08:25:06
问题 I have a sample dataset: id <- 1:100 gender <- sample(c('M','F'), 100, replace=TRUE) age <- sample(18:22, 100, replace=TRUE) ethnicity <- sample(c('W','B','H','A','O'), 100, replace = TRUE) grade <- sample(LETTERS[1:4], 100, replace=TRUE) df <- cbind(id,gender,age,ethnicity,grade) %>% as.data.frame() My output I'm trying to achieve is as such: +-------------+-------+----+----+----+----+ | Column Name | Value | A | B | C | D | +-------------+-------+----+----+----+----+ | Gender | F | 15 | 11

Pivotting multiple columns with literal names_pattern [duplicate]

我与影子孤独终老i 提交于 2020-01-24 19:44:11
问题 This question already has answers here : Reshaping data.frame from wide to long format (7 answers) Closed last month . I want to pivot_longer() my data frame to contain a column with the row number of the original data. Consider the following data frame: set.seed(0) df <- data.frame( a = runif(3), b = runif(3), c = runif(3) ) %>% mutate("row_id" = row_number()) > df a b c row_id 1 0.8966972 0.5728534 0.8983897 1 2 0.2655087 0.9082078 0.9446753 2 3 0.3721239 0.2016819 0.6607978 3 The desired

Wide to long data transformation multiple columns [duplicate]

血红的双手。 提交于 2020-01-24 17:07:07
问题 This question already has answers here : Reshaping multiple sets of measurement columns (wide format) into single columns (long format) (7 answers) Closed last month . I have data frame df_wide for company data in a wide format df_wide <- data.frame(Company=c('CompanyA','CompanyB', 'CompanyC'), Industry=c('Manufacturing', 'Telecom', 'Services'), Sales.2015=c('100', '500', '1000'), Sales.2016=c('110', '550', '1100'), Sales.2017=c('120', '600', '1200'), EBITDA.2015=c('10', '50', '100'), EBITDA

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>

Excluding column from gather() from tidyr package using standard evaluation

拜拜、爱过 提交于 2020-01-24 07:26:33
问题 I'm trying to exclude a column from gather() from the tidyr package using standard evaluation. Using non-standard evaluation, this works fine: mtcars_df <- head(mtcars[, c("mpg", "hp", "cyl")]) tidyr::gather(mtcars_df, key, val, -cyl) However, using standard evaluation, neither of the following work and both return an error: tidyr::gather_(mtcars_df, "key", "val", -"cyl") tidyr::gather_(mtcars_df, "key", "val", -cyl) As a work-around, I tried to use the select() helper function contains() ,