tidyverse

R - How can I add an empty POSIXct column to a data.frame / tibble which already exists?

拟墨画扇 提交于 2019-12-02 03:10:40
问题 I can initialize a data frame with a POSIXct column with code like this: df <- data.frame(a=numeric(), b=character(), c=as.POSIXct(character())) However, if I try to add an empty POSIXct column to a data.frame or tibble which already exists, the column is transformed to numeric type/class. > df <- tibble("Index"=numeric(10)) > df[,"date"] <- as.POSIXct(character()) > df[,"date"] %>% pull %>% class() [1] "numeric Is there a method to overcome this problem? 回答1: would this work for you (most

Column name of last non-NA row per row; using tidyverse solution?

假装没事ソ 提交于 2019-12-02 02:42:13
Brief Dataset description: I have survey data generated from Qualtrics, which I've imported into R as a tibble. Each column corresponds to a survey question, and I've preserved the original column order (to correspond with the order of the questions in the survey). Problem in plain language: Due to normal participant attrition, not all participants completed all of the questions in the survey. I want to know how far each participant got in the survey, and the last question they each answered before stopping. Problem statement in R: I want to generate (using tidyverse): 1) A new column ( lastq

Pivot wider produces nested object

随声附和 提交于 2019-12-02 00:57:54
This is regarding latest tidyr release. I am trying pivot_wider & pivot_longer function from library(tidyr) (Update 1.0.0) I was trying to obtain normal iris dataset when I run below but instead I get nested sort of 3X5 dimension tibble, not sure whats happening (I read https://tidyr.tidyverse.org/articles/pivot.html ) but still not sure how to avoid this library(tidyr) iris %>% pivot_longer(-Species,values_to = "count") %>% pivot_wider(names_from = name, values_from = count) Expected Output: Normal Iris dataset (150 X 5 dimension) Edit: I read below that if I wrap around unnest() I get

R - How can I add an empty POSIXct column to a data.frame / tibble which already exists?

久未见 提交于 2019-12-02 00:55:21
I can initialize a data frame with a POSIXct column with code like this: df <- data.frame(a=numeric(), b=character(), c=as.POSIXct(character())) However, if I try to add an empty POSIXct column to a data.frame or tibble which already exists, the column is transformed to numeric type/class. > df <- tibble("Index"=numeric(10)) > df[,"date"] <- as.POSIXct(character()) > df[,"date"] %>% pull %>% class() [1] "numeric Is there a method to overcome this problem? would this work for you (most doing what eipi10 suggest in his comment ) library(tibble) # install.packages(c("dplyr"), dependencies = TRUE)

R: Join two tables (tibbles) by *list* columns

拥有回忆 提交于 2019-12-02 00:19:47
问题 Seems like there should be a simple answer for this but I haven't been able to find one: tib1 <- tibble(x = list(1, 2, 3), y = list(4, 5, 6)) tib1 # A tibble: 3 × 2 x y <list> <list> 1 <dbl [1]> <dbl [1]> 2 <dbl [1]> <dbl [1]> 3 <dbl [1]> <dbl [1]> tib2 <- tibble(x = list(1, 2, 4, 5), y = list(4, c(5, 10), 6, 7)) tib2 # A tibble: 4 × 2 x y <list> <list> 1 <dbl [1]> <dbl [1]> 2 <dbl [1]> <dbl [2]> 3 <dbl [1]> <dbl [1]> 4 <dbl [1]> <dbl [1]> dplyr::inner_join(tib1, tib2) Joining, by = c("x", "y

How to import Qualtrics data (in csv format) into R

梦想的初衷 提交于 2019-12-01 20:42:30
问题 I am trying to import a data downloaded from Qualtrics into R. It is a csv file. However, I encounter 2 problems. R could not figure out the format of each column by itself, probably because row 2 and row 3 (highlighted above) are all useless text. R thinks that all columns are character . However, obviously some are date , some are factor , and some are integer . How can R figure out the data class of each column correctly by itself? library(tidyverse) filename <- "mydata.csv" df = read_csv

How to import Qualtrics data (in csv format) into R

戏子无情 提交于 2019-12-01 19:55:08
I am trying to import a data downloaded from Qualtrics into R. It is a csv file. However, I encounter 2 problems. R could not figure out the format of each column by itself, probably because row 2 and row 3 (highlighted above) are all useless text. R thinks that all columns are character . However, obviously some are date , some are factor , and some are integer . How can R figure out the data class of each column correctly by itself? library(tidyverse) filename <- "mydata.csv" df = read_csv(filename, col_names = TRUE) Parsed with column specification: cols( .default = col_character() ) See

How to remove rows where all columns are zero using dplyr pipe

喜欢而已 提交于 2019-12-01 18:14:41
问题 I have the following data frame: dat <- structure(list(`A-XXX` = c(1.51653275922944, 0.077037240321129, 0), `fBM-XXX` = c(2.22875185527511, 0, 0), `P-XXX` = c(1.73356698481106, 0, 0), `vBM-XXX` = c(3.00397859609183, 0, 0)), .Names = c("A-XXX", "fBM-XXX", "P-XXX", "vBM-XXX"), row.names = c("BATF::JUN_AHR", "BATF::JUN_CCR9", "BATF::JUN_IL10"), class = "data.frame") dat #> A-XXX fBM-XXX P-XXX vBM-XXX #> BATF::JUN_AHR 1.51653276 2.228752 1.733567 3.003979 #> BATF::JUN_CCR9 0.07703724 0.000000 0

How to use dplyr programming syntax to create and evaluate variable names

牧云@^-^@ 提交于 2019-12-01 18:05:58
I would like to dynamically input a variable name using dplyr programming syntax, however, as many have described this can be quite confusing. I've played around with various combinations of quo/enquo !! etc. to no avail. Here is the simplest form of my code library(tidyverse) df <- tibble( color1 = c("blue", "blue", "blue", "blue", "blue"), color2 = c("black", "black", "black", "black", "black"), value = 1:5 ) num <- 2 df %>% mutate(color3 = !!(paste0("color", num))) #> # A tibble: 5 x 4 #> color1 color2 value color3 #> <chr> <chr> <int> <chr> #> 1 blue black 1 color2 #> 2 blue black 2 color2

How to remove rows where all columns are zero using dplyr pipe

左心房为你撑大大i 提交于 2019-12-01 18:05:51
I have the following data frame: dat <- structure(list(`A-XXX` = c(1.51653275922944, 0.077037240321129, 0), `fBM-XXX` = c(2.22875185527511, 0, 0), `P-XXX` = c(1.73356698481106, 0, 0), `vBM-XXX` = c(3.00397859609183, 0, 0)), .Names = c("A-XXX", "fBM-XXX", "P-XXX", "vBM-XXX"), row.names = c("BATF::JUN_AHR", "BATF::JUN_CCR9", "BATF::JUN_IL10"), class = "data.frame") dat #> A-XXX fBM-XXX P-XXX vBM-XXX #> BATF::JUN_AHR 1.51653276 2.228752 1.733567 3.003979 #> BATF::JUN_CCR9 0.07703724 0.000000 0.000000 0.000000 #> BATF::JUN_IL10 0.00000000 0.000000 0.000000 0.000000 I can remove the row with all