tidyr

Go from long to wide using tidyr's pivot_wider

a 夏天 提交于 2021-02-20 04:26:28
问题 I have a simple long df where every element in the fi column should be a new column in the wide df. So the new df should have 10 columns A:J. The wide df should have two rows, "one" and "two". Sounds like a job for pivot_wider, but I couldn't get it to work. library("tidyverse") df <- structure(list(fi = c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J"), one = c(0.5, 1.4, 0.89, 1.4, 1.45, 1.25, 1.45, 1.4, 1.4, 1.5), two = c(0.75, 1.6, 1.05, 1.6, 1.45, 1.05, 1.65, 1.5, 1.55, 1.65)), row

R pivot_longer combining columns based on the end of column names

时间秒杀一切 提交于 2021-02-20 02:44:39
问题 I have a dataframe with multiple columns with names of various lengths and structures (so not sure how to capture them with a regex). Each column ends either with .t1 or .t3 I want to combine columns based on names without the t1/t3, with an additional column of Time based on that suffix. So, for example, a dataframe such as: df<-data.frame("Subject"= c(1:10), "intercept.freq.acc.t1" = c(1:10), "intercept.freq.acc.t3" = c(1:10), "freq.rt.t1" = c(1:10), "freq.rt.t3" = c(1:10), "vowel.con.acc

R pivot_longer combining columns based on the end of column names

房东的猫 提交于 2021-02-20 02:43:26
问题 I have a dataframe with multiple columns with names of various lengths and structures (so not sure how to capture them with a regex). Each column ends either with .t1 or .t3 I want to combine columns based on names without the t1/t3, with an additional column of Time based on that suffix. So, for example, a dataframe such as: df<-data.frame("Subject"= c(1:10), "intercept.freq.acc.t1" = c(1:10), "intercept.freq.acc.t3" = c(1:10), "freq.rt.t1" = c(1:10), "freq.rt.t3" = c(1:10), "vowel.con.acc

How to pivot a single cell dataframe

非 Y 不嫁゛ 提交于 2021-02-19 12:52:32
问题 I have encountered such a simple challenge, and yet don't know how to do this properly. library(tibble) library(dplyr) # I have this single-cell dataframe tibble::tribble(~red, "apple") ## # A tibble: 1 x 1 ## red ## <chr> ## 1 apple But being red is a property of the variable fruit , which apple is one observation of. Therefore, I want my data to look like: # Desired Output: ## # A tibble: 1 x 2 ## fruit red ## <chr> <lgl> ## 1 apple TRUE So I tried a clunky method, which seems not best

How to pivot a single cell dataframe

↘锁芯ラ 提交于 2021-02-19 12:51:36
问题 I have encountered such a simple challenge, and yet don't know how to do this properly. library(tibble) library(dplyr) # I have this single-cell dataframe tibble::tribble(~red, "apple") ## # A tibble: 1 x 1 ## red ## <chr> ## 1 apple But being red is a property of the variable fruit , which apple is one observation of. Therefore, I want my data to look like: # Desired Output: ## # A tibble: 1 x 2 ## fruit red ## <chr> <lgl> ## 1 apple TRUE So I tried a clunky method, which seems not best

How to pivot a single cell dataframe

本秂侑毒 提交于 2021-02-19 12:49:13
问题 I have encountered such a simple challenge, and yet don't know how to do this properly. library(tibble) library(dplyr) # I have this single-cell dataframe tibble::tribble(~red, "apple") ## # A tibble: 1 x 1 ## red ## <chr> ## 1 apple But being red is a property of the variable fruit , which apple is one observation of. Therefore, I want my data to look like: # Desired Output: ## # A tibble: 1 x 2 ## fruit red ## <chr> <lgl> ## 1 apple TRUE So I tried a clunky method, which seems not best

Is there more efficient or concise way to use tidyr::gather to make my data look 'tidy'?

前提是你 提交于 2021-02-18 16:59:43
问题 I am new to using tidyverse. I want to see if I am being as efficient/concise as possible using the functions in this package. I suspect I am not. My original data has the key sym as part of each column name. day a_x b_x a_y b_y 1 1 -0.56047565 1.2240818 -1.0678237 0.42646422 2 2 -0.23017749 0.3598138 -0.2179749 -0.29507148 ... I would like to make the data look tidy, like so: day sym x y 1 1 a 0.118 0.702 2 2 a -0.947 -0.262 ... 11 1 b 1.44 0.788 12 2 b 0.452 0.769 Here is my code that does

Is there more efficient or concise way to use tidyr::gather to make my data look 'tidy'?

爷,独闯天下 提交于 2021-02-18 16:59:12
问题 I am new to using tidyverse. I want to see if I am being as efficient/concise as possible using the functions in this package. I suspect I am not. My original data has the key sym as part of each column name. day a_x b_x a_y b_y 1 1 -0.56047565 1.2240818 -1.0678237 0.42646422 2 2 -0.23017749 0.3598138 -0.2179749 -0.29507148 ... I would like to make the data look tidy, like so: day sym x y 1 1 a 0.118 0.702 2 2 a -0.947 -0.262 ... 11 1 b 1.44 0.788 12 2 b 0.452 0.769 Here is my code that does

Unnesting a data frame containing lists

蹲街弑〆低调 提交于 2021-02-16 20:33:52
问题 I have a data frame that contains lists, like below: # Load packages library(dplyr) # Create data frame df <- structure(list(ID = 1:3, A = structure(list(c(9, 8), c(7,6), c(6, 9)), ptype = numeric(0), class = c("vctrs_list_of", "vctrs_vctr")), B = structure(list(c(3, 5), c(2, 6), c(1, 5)), ptype = numeric(0), class = c("vctrs_list_of", "vctrs_vctr")), C = structure(list(c(6, 5), c(7, 6), c(8, 7)), ptype = numeric(0), class = c("vctrs_list_of", "vctrs_vctr")), D = structure(list(c(5, 3), c(4,

Separate string into many columns

元气小坏坏 提交于 2021-02-16 18:23:06
问题 I'd like to separate each letter or symbol in a string for composing a new data.frame with dimension equals the number of letters. I tried to use the function separate from tidyr package, but the result is not desired. df <- data.frame(x = c('house', 'mouse'), y = c('count', 'apple'), stringsAsFactors = F) unexpected result df[1, ] %>% separate(x, c('A1', 'A2', 'A3', 'A4', 'A5'), sep ='') A1 A2 A3 A4 A5 y 1 <NA> <NA> <NA> <NA> <NA> count Expected output A1 A2 A3 A4 A5 h o u s e m o u s e