tidyr

Using separate from tidyr with different length vectors

老子叫甜甜 提交于 2019-12-23 13:06:34
问题 I would like to separate a column of strings such as [1, 58, 10] into columns using separate from tidyr. My problem is that sometimes the columns are shorter (never longer). I have many columns with this issue in the same data frame. Loading packages require(tidyr) require(dplyr) require(stringr) The data Here I make a data frame with samples from the real data. The "vectors" are of length 10 in col1 and 9 or 10 in col2. There is a time column just to show that there are other columns as well

Using separate from tidyr with different length vectors

陌路散爱 提交于 2019-12-23 13:06:29
问题 I would like to separate a column of strings such as [1, 58, 10] into columns using separate from tidyr. My problem is that sometimes the columns are shorter (never longer). I have many columns with this issue in the same data frame. Loading packages require(tidyr) require(dplyr) require(stringr) The data Here I make a data frame with samples from the real data. The "vectors" are of length 10 in col1 and 9 or 10 in col2. There is a time column just to show that there are other columns as well

Sampling different numbers of rows by group in dplyr tidyverse

▼魔方 西西 提交于 2019-12-23 12:53:33
问题 I'd like to sample rows from a data frame by group. But here's the catch, I'd like to sample a different number of records based on data from another table. Here is my reproducible data: df <- data_frame( Stratum = rep(c("High","Medium","Low"), 10), id = c(1:30), Value = runif(30) ) sampleGuide <- data_frame( Stratum = c("High","Medium","Low"), Surveys = c(3,2,5) ) Output should look like this: # A tibble: 10 × 2 Stratum Value <chr> <dbl> 1 High 0.21504972 2 High 0.71069005 3 High 0.09286843

Combining multiple rows into one row with multiple columns of data R

和自甴很熟 提交于 2019-12-23 12:52:07
问题 I have a dataset where the same effect was measured in different ways and I want to compare those measurements. My dataset looks like this: Study MType ID Insect Mean Sd N Alla Fecundity 1 Aphid .62 .7628 11 Alla RGR 1 Aphid -32.8 7.76 11 Ando Survival 2 Bee 2.34 .67 8 Ando RGR 2 Bee 4.56 .34 10 Ando Fecundity 2 Bee 5.32 4.3 20 I want to combine the rows by ID number so that the MType, Mean, Sd and N for each row are preserved (although the column names need to change so the columns are

Can't change the column names outputted by “gather” to be anything other than the default names

半世苍凉 提交于 2019-12-23 12:17:14
问题 I'm trying to use gather in the tidyr package, but I'm not able to change the outputted column names from the default names. For instance: df = data.frame(time = 1:100,a = 1:100,b = 101:200) df.long = df %>% gather("foo","bar",a:b) colnames(df.long) gives me [1] "time" "variable" "value" but shouldn't it be "time" "foo" "bar" ? I can change "foo" and "bar" to anything I want, and it still gives me "variable" and "value" as my column names. Help. What am I missing here? 回答1: This could

Using Tidyr/Dplyr to summarise counts of groups of strings

∥☆過路亽.° 提交于 2019-12-23 11:59:07
问题 I need to summarise the counts of strings I am assigning to groups, and I know I can do it in dplyr/tidyr but I am missing something. Example dataset: Owner = c('bob','julia','cheryl','bob','julia','cheryl') Day = c('Mon', 'Tue') Locn = c('house','store','apartment','office','house','shop') data <- data.frame(Owner, Day, Locn) which looks like this: Owner Day Locn 1 bob Mon house 2 julia Tue store 3 cheryl Mon apartment 4 bob Tue office 5 julia Mon house 6 cheryl Tue shop I want to group by

Unnest one of several list columns in dataframe

蹲街弑〆低调 提交于 2019-12-23 11:14:13
问题 I have a tibble with several list columns and I'd like to only unnest one of them. Example library(dplyr) library(purrr) library(tidyr) library(stringr) iris %>% group_by(Species) %>% nest() %>% mutate(sum_data = map(data, ~.x %>% summarize_all(mean) %>% rename_all(funs(str_c("Mean.", .))))) # A tibble: 3 x 3 # Species data sum_data # <fctr> <list> <list> # 1 setosa <tibble [50 x 4]> <tibble [1 x 4]> # 2 versicolor <tibble [50 x 4]> <tibble [1 x 4]> # 3 virginica <tibble [50 x 4]> <tibble [1

Unnest one of several list columns in dataframe

无人久伴 提交于 2019-12-23 11:11:24
问题 I have a tibble with several list columns and I'd like to only unnest one of them. Example library(dplyr) library(purrr) library(tidyr) library(stringr) iris %>% group_by(Species) %>% nest() %>% mutate(sum_data = map(data, ~.x %>% summarize_all(mean) %>% rename_all(funs(str_c("Mean.", .))))) # A tibble: 3 x 3 # Species data sum_data # <fctr> <list> <list> # 1 setosa <tibble [50 x 4]> <tibble [1 x 4]> # 2 versicolor <tibble [50 x 4]> <tibble [1 x 4]> # 3 virginica <tibble [50 x 4]> <tibble [1

how to create new variables from one variable using two rules

只谈情不闲聊 提交于 2019-12-23 05:59:57
问题 I would appreciate any help to create new variables from one variable. Specifically, I need help to simultaneously create one row per each ID and various columns of E , where each of the new columns of E , (that is, E1 , E2 , E3 ) contains the values of E for each row of ID . I tried doing this which melt followed by spread but I am getting the error: Error: Duplicate identifiers for rows (4, 7, 9), (1, 3, 6), (2, 5, 8) Additionally, I tried the solutions discussed here and here but these did

Create new columns with dummies based on values [duplicate]

让人想犯罪 __ 提交于 2019-12-23 02:42:11
问题 This question already has answers here : Create several dummy variables from one string variable (4 answers) Aggregating multiple columns from data frame [duplicate] (4 answers) Closed last year . I want to make new columns, based on the values of a single existing column. It is event data (from a website), so the number of values are different. Just like this: row Events 1 237,2,236,102,106,111,114,115,116,117,118,119,125 2 237,111,116 3 102,106,111,114,115 4 237,2,236,102,106,111,114,115