tidyr

Spread multiple columns with values by one columne containing key

╄→гoц情女王★ 提交于 2019-12-08 05:38:34
问题 Input data : > head(iris[c(48:50, 98:100), 3:5]) Petal.Length Petal.Width Species 48 1.4 0.2 setosa 49 1.5 0.2 setosa 50 1.4 0.2 setosa 98 4.3 1.3 versicolor 99 3.0 1.1 versicolor 100 4.1 1.3 versicolor Output data : setosa.Petal.Length versicolor.Petal.Length setosa.Petal.Width versicolor.Petal.Width 1.4 4.3 0.2 1.3 1.5 3.0 0.2 1.1 1.4 4.1 0.2 1.3 using for example : spread( iris%>%mutate(n=row_number()), key=Species, value=Petal.Length:Petal.Width) #or c("Petal.Length", "Petal.Width")`

how to create categories conditionally using other variables values and sequence

孤街浪徒 提交于 2019-12-08 05:09:52
问题 I would appreciate any help to create a function that allows me to create categories of one variable using the order of a set of other variables values. Specifically, I want a function that: creates category E1 of the variable variable the first time that each combination of values of the variables A , B , and ID appears in the dataset. creates category E2 of the variable variable the second time that each combination of values of the variables A , B , and ID appears in the dataset. creates

dplyr - sum of multiple columns using regular expressions

痞子三分冷 提交于 2019-12-08 04:19:53
问题 For the dataset mtcars2 mtcars2 = mtcars mtcars2 = mtcars2 %>% mutate(cyl9=cyl, disp9=disp, gear2=gear) I want to get a new column which is the sum of multiple columns, by using regular expressions to capture the pattern. This is a solution, however this is done by hard-coding select(mtcars2, cyl9) + select(mtcars2, disp9) + select(mtcars2, gear2) I tried something like this but it gives me a number instead of a vector mtcars2 %>% select(matches("[0-9]")) %>% sum Please dplyr solutions only,

Unable to use tidyselect `everything()` in combination with `group_by()` and `fill()`

混江龙づ霸主 提交于 2019-12-07 18:33:42
问题 library(tidyverse) df <- tibble(x1 = c("A", "A", "A", "B", "B", "B"), x2 = c(NA, 8, NA, NA, NA, 5), x3 = c(3, 6, 5, 9, 1, 9)) #> # A tibble: 6 x 3 #> x1 x2 x3 #> <chr> <dbl> <dbl> #> 1 A NA 3 #> 2 A 8 NA #> 3 A NA 5 #> 4 B NA 9 #> 5 B NA 1 #> 6 B 5 9 I have groups 'A' and 'B' shown in column x1 . I need the 'NA' values in columns x2 and x3 to populate only from values within the same group, in the updown direction. That's simple enough, here's the code: df %>% group_by(x1) %>% fill(c(x2, x3),

Separate variable in field by character

落爺英雄遲暮 提交于 2019-12-07 16:26:59
问题 I recently asked this question Separate contents of field And got a very quick and very simple answer. Something I can do simply in Excel is look in a cell, find the first instance of a character and then return all the characters to the left of that. For example Author Drijgers RL, Verhey FR, Leentjens AF, Kahler S, Aalten P. I can extract Drijgers RL and Aalten P into separate columns in excel. This lets me count the number of times someone is a first author and also the last author. How

R: How to keep names while unnesting doubled nested tibble?

回眸只為那壹抹淺笑 提交于 2019-12-07 15:46:32
问题 at the moment I'm trying to figure out how to keep the names of an inner and other list nested within a tibble while unnesting. The .id parameter of the unnest function is the closest I found, but it starts to number the values instead of using the given names. here is a MWE with my idea of the final tibble: library(dplyr) library(tidyr) df.1 <- tibble( x = list("Foo","Bar"), y = list( list(a = list(aa = 1, ab = 2), b = list(ba = 6, bb = 22)), list(c = list(ca = 561, cb = 35), d = list(da =

Separate string after last underscore

给你一囗甜甜゛ 提交于 2019-12-07 15:28:57
问题 This is indeed a duplicate for this question r-split-string-using-tidyrseparate, but I cannot use the MWE for my purpose, because I do not know how to adjust the regular Expression. I basically want the same thing, but split the variable after the last underscore. Reason: I have data where some columns show up several times for the same factor/type. I figured I can melt the data separate the value variable before the type string and spread it out again to a wide format with less columns. My

Concatenating all rows within a group using dplyr

人盡茶涼 提交于 2019-12-07 15:22:39
问题 Suppose I have a dataframe like this: hand_id card_id card_name card_class A 1 p alpha A 2 q beta A 3 r theta B 2 q beta B 3 r theta B 4 s gamma C 1 p alpha C 2 q beta I would like to concatenate the card_id, card_name, and card_class into one single row per hand level A, B, C. So the result would look something like this: hand_id combo_1 combo_2 combo_3 A 1-2-3 p-q-r alpha-beta-theta B 2-3-4 q-r-s beta-theta-gamma .... I attempted to do this using group_by and mutate, but I can't seem to get

Unnest one column list to many columns in tidyr

空扰寡人 提交于 2019-12-07 12:58:04
问题 For example, I have a tidy data frame like this: df <- tibble(id=1:2, ctn=list(list(a="x",b=1), list(a="y",b=2))) # A tibble: 2 x 2 id ctn <int> <list> 1 1 <list [2]> 2 2 <list [2]> How could I unnest ctn column to the right so that the data frame will be like this: # A tibble: 2 x 3 id a b <int> <chr> <dbl> 1 1 x 1 2 2 y 2 回答1: With dplyr and purrr df %>% mutate(ctn = map(ctn, as_tibble)) %>% unnest() # A tibble: 2 x 3 id a b <int> <chr> <dbl> 1 1 x 1 2 2 y 2 回答2: One option is library(data

Gather multiple groups of columns in R [duplicate]

回眸只為那壹抹淺笑 提交于 2019-12-07 05:00:46
问题 This question already has answers here : Combine Multiple Columns Into Tidy Data [duplicate] (3 answers) Closed 2 years ago . I have a wide dataframe that I need to gather or melt into a tall dataframe. The part that I'm stuck on is that I have groups of columns that need to remain associated/grouped. I have 2 users for each form submission and 3 columns of data for each user. I'd like to take these 6 columns and essentially stack them in groups of 3 so that each user is a separate