tidyr

How to extract particular values from a factor store as column value in dataframe

和自甴很熟 提交于 2020-03-05 01:31:00
问题 I have a data frame with the following columns : df <- A B C heart_rate ['53.0', '1'] 94 heart_rate ['54.0', '2'] 1 heart_rate ['54.0', '1'] 9 heart_rate ['55.0', '0'] 1 heart_rate ['55.0', '1'] 7 How to read the df1 which just stores one value for cases where we have y= 1 for B[x,y].Which means : Output desired dataframe df1 df1 A B C heart_rate 53.0 94 heart_rate 54.0 9 heart_rate 55.0 7 structure(list(source = structure(c(1L, 1L, 1L), .Label = "heart_rate", class = "factor"), values =

Gather variables based on condition (R)

∥☆過路亽.° 提交于 2020-02-25 04:05:31
问题 I'm working through the book "R for Data Science" and would like to "gather" several variables from the dataset based on a condition (similar to select). Specifically, I want to pick just the continuous variables not the categorical ones. How can I accomplish this without manually specifying the variables? Below does not work... library(tidyverse) diamonds %>% gather(key, value, is.numeric(key)) 回答1: I'm sure there are better ways to do this but gather() can take column positions as the

Gather variables based on condition (R)

五迷三道 提交于 2020-02-25 04:04:29
问题 I'm working through the book "R for Data Science" and would like to "gather" several variables from the dataset based on a condition (similar to select). Specifically, I want to pick just the continuous variables not the categorical ones. How can I accomplish this without manually specifying the variables? Below does not work... library(tidyverse) diamonds %>% gather(key, value, is.numeric(key)) 回答1: I'm sure there are better ways to do this but gather() can take column positions as the

Mutate based on two conditions in R dataframe

江枫思渺然 提交于 2020-02-24 11:43:11
问题 I have a R dataframe which can be generated from the code below DF <- data.frame("Person_id" = c(1,1,1,1,2,2,2,2,3,3), "Type" = c("IN","OUT","IN","ANC","IN","OUT","IN","ANC","EM","ANC"), "Name" = c("Nara","Nara","Nara","Nara","Dora","Dora","Dora","Dora","Sara","Sara"),"day_1" = c("21/1/2002","21/4/2002","21/6/2002","21/9/2002","28/1/2012","28/4/2012","28/6/2012","28/9/2012","30/06/2004","30/06/2005"),"day_2" = c("23/1/2002","21/4/2002","","","30/1/2012","28/4/2012","","28/9/2012","","")) What

How to transpose a dataframe in tidyverse?

扶醉桌前 提交于 2020-02-17 06:50:41
问题 Using basic R, I can transpose a dataframe, say mtcars , which has all columns of the same class: as.data.frame(t(mtcars)) Or with pipes: library(magrittr) mtcars %>% t %>% as.data.frame How to accomplish the same within tidyr or tidyverse packages? My attempt below gives: Error: Duplicate identifiers for rows library(tidyverse) mtcars %>% gather(var, value, everything()) %>% spread(var, value) 回答1: Try with add_rownames add_rownames(mtcars) %>% gather(var, value, -rowname) %>% spread(rowname

How to transpose a dataframe in tidyverse?

核能气质少年 提交于 2020-02-17 06:50:13
问题 Using basic R, I can transpose a dataframe, say mtcars , which has all columns of the same class: as.data.frame(t(mtcars)) Or with pipes: library(magrittr) mtcars %>% t %>% as.data.frame How to accomplish the same within tidyr or tidyverse packages? My attempt below gives: Error: Duplicate identifiers for rows library(tidyverse) mtcars %>% gather(var, value, everything()) %>% spread(var, value) 回答1: Try with add_rownames add_rownames(mtcars) %>% gather(var, value, -rowname) %>% spread(rowname

What is the meaning of “ \\.” in tidyr::separate?

醉酒当歌 提交于 2020-02-16 08:16:58
问题 what is the purpose of " \. " and why it is quoted? this is the code: library(tidyr) iris.tidy <- iris %>% gather(key, Value, -Species) %>% separate(key, c("Part", "Measure"), "\\.") it is for the iris dataset 回答1: It would be easier to understand if you run the code step by step. gather brings the data in long format with column key with column names and column value with values of those columns library(tidyr) iris %>% gather(key, Value, -Species) %>% head # Species key Value #1 setosa Sepal

tidyr: Gathering two values per key [duplicate]

梦想的初衷 提交于 2020-02-04 05:19:10
问题 This question already has answers here : Reshaping multiple sets of measurement columns (wide format) into single columns (long format) (7 answers) Gather multiple sets of columns [duplicate] (5 answers) Closed 6 months ago . I have a dataset with the mean and sd of each variable as columns, but I want to convert it into "long" format as so: library(tidyverse) iris %>% group_by(Species) %>% summarize_all(list(mean = mean, sd = sd)) #> # A tibble: 3 x 9 #> Species Sepal.Length_me~ Sepal.Width

r - gather multiple columns in multiple key columns with tidyr

依然范特西╮ 提交于 2020-02-02 13:49:50
问题 Not sure if tidyr::gather can be used to take multiple columns and merge them in multiple key columns. Similar questions have been asked but they all refer to gathering multiple columns in one key column. I'm trying to gather 4 columns into 2 key and 2 value columns like in the following example: Sample data: df <- data.frame( subject = c("a", "b"), age1 = c(33, 35), age2 = c(43, 45), weight1 = c(90, 67), weight2 = c(70, 87) ) subject age1 age2 weight1 weight2 1 a 33 43 90 70 2 b 35 45 67 87

Transforming a nested data frame with varying number of elements

旧街凉风 提交于 2020-02-02 04:38:06
问题 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: .. .