tidyverse

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

Iterating over multiple regression models and data subsets in R

北慕城南 提交于 2020-02-03 12:16:08
问题 I am trying to learn how to automate running 3 or more regression models over subsets of a dataset using the purrr and broom packages in R. I am doing this with the nest %>% mutate(map()) %>% unnest() flow in mind. I am able to replicate examples online when there is only one regression model that is applied to several data subsets. However, I am running into problems when I have more than one regression model in my function. What I tried to do library(tidyverse) library(broom) estimate_model

Iterating over multiple regression models and data subsets in R

允我心安 提交于 2020-02-03 12:15:27
问题 I am trying to learn how to automate running 3 or more regression models over subsets of a dataset using the purrr and broom packages in R. I am doing this with the nest %>% mutate(map()) %>% unnest() flow in mind. I am able to replicate examples online when there is only one regression model that is applied to several data subsets. However, I am running into problems when I have more than one regression model in my function. What I tried to do library(tidyverse) library(broom) estimate_model

dbplyr copy_to not saving table to database

你。 提交于 2020-02-02 15:05:40
问题 I am trying to copy a local dataframe from R to my db2 database. I have permissions to write to the table and I have verified the connection is working. I am using: copy_to(connection, data.frame, name = my_table_name) I am getting the following error and it doesnt make sense to me. The object it says does not exist is the very object I am trying to create. What am I doing wrong? Error in typeof(x) : object 'my_table_name' not found 回答1: by default, copy_to() tries to create a temporary table

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)

How to convert list of list into a tibble (dataframe)

陌路散爱 提交于 2020-01-29 07:08:04
问题 I have the following list of list. It contains two variables: pair and genes. The contain of pair is always vector with two strings. And the variable genes is a vector which can contain more than 1 values. lol <- list(structure(list(pair = c("BoneMarrow", "Pulmonary"), genes = "PRR11"), .Names = c("pair", "genes")), structure(list(pair = c("BoneMarrow", "Umbilical"), genes = "GNB2L1"), .Names = c("pair", "genes")), structure(list( pair = c("Pulmonary", "Umbilical"), genes = "ATP1B1"), .Names

How would I be able to remove opposite values (e.g. refunds) in panel data?

我是研究僧i 提交于 2020-01-25 08:00:07
问题 Given the following data: id|datee | price | quant | discrete_x 1 2018-12-19 4 -3000 A 1 2018-12-04 4 3000 A 1 2018-12-21 4 3000 B 1 2018-12-20 3 2000 A ... Desired output: id|datee | price | quant | discrete_x 1 2018-12-21 4 3000 B 1 2018-12-20 3 2000 A ... In this case, it is quite clear that the quantity ( quant ) of 3000 is refunded, then bought again. I would like to remove the two rows for cancelling each other out. Given that id and quant match while the refund happens once and after a

Encoding issue in tidyverse startup message in RStudio: distorted UTF-8 output (<U+2713>)

a 夏天 提交于 2020-01-24 15:11:25
问题 Some time ago everything was ok. But after recent updates (I cannot track down which ones), in RStudio the "Attaching packages" section of the tidyverse startup message has encoding issues while the "Conflicts" part is correct. In R Gui this issue is not present (and there are no different colors). How can I get a non-distorted tidyverse startup message in RStudio? (With v and not <U+2713> ). Is it a bug in some package or RStudio? Are there some settings incorrect on my side? Rstudio version

Estimating multiple `lm` models and returning output in one table, with map()

半城伤御伤魂 提交于 2020-01-24 12:33:16
问题 I need to estimate a number of linear models on the same dataset, and put the regression results all into one table. For a reproducible example, here's a simplification using mtcars : formula_1 = "mpg ~ disp" formula_2 = "mpg ~ log(disp)" formula_3 = "mpg ~ disp + hp" Currently, my approach has been to: Create a list that contains all of the formulae. use purrr:map() to estimate all of the lm models. use stargazer:: to produce output tables. library(tidyverse) library(stargazer) formula_1 =