tidyverse

How to reshape a wider data.frame to longer data.frame in R? [duplicate]

半世苍凉 提交于 2021-02-10 22:15:51
问题 This question already has answers here : Transpose and Merge columns in R [duplicate] (3 answers) Reshaping data.frame from wide to long format (9 answers) Closed 7 months ago . I was playing with pivot_longer and pivot_wider but probably am missing something. I have a data.frame like D_Wider and would like to convert it to something like D_longer . any way forward? library(tidyverse) D_Wider <- data.frame(A = 15, S = 10, D = 25, Z = 16) Desired Output D_Longer <- data.frame(Stations = c("A",

R: Use Regex to Import Specific Sheets from Multiple Excel Files

笑着哭i 提交于 2021-02-10 15:57:02
问题 I have a group of Excel files with multiple sheets which don’t follow a standard naming convention. I want to create a single data frame from specific sheets containing the keyword 'frame' . library(tidyverse) library(openxlsx) # Sample Excel File 1 df1 <- data.frame(replicate(10,sample(0:1,10,rep=TRUE))) data_frame2 <- data.frame(replicate(10,sample(0:1,10,rep=TRUE))) list_of_datasets1 <- list("df" = df1, "date_frame" = data_frame2) write.xlsx(list_of_datasets1, file = "writeXLSX1.xlsx") #

R - merge 2 dataframes with timestamp between

 ̄綄美尐妖づ 提交于 2021-02-10 14:41:03
问题 DF1: structure(list(X = c(113.8577674, 113.8577537, 113.8577403), Y = c(22.19537297, 22.19537222, 22.1953723), Date = c("7/1/2016", "7/1/2016", "7/1/2016"), Time = structure(c(9474, 9484, 9494 ), class = c("hms", "difftime"), units = "secs"), TrackTime = structure(c(38274, 38284, 38294), class = c("hms", "difftime"), units = "secs")), .Names = c("X", "Y", "Date", "Time", "TrackTime"), row.names = c(NA, -3L), class = "data.frame", spec = structure(list( cols = structure(list(X = structure(list

Joining dataframes from lists of unequal length

白昼怎懂夜的黑 提交于 2021-02-10 06:15:19
问题 I have two lists of dataframes that I want to combine, I have tried the following: A <- data.frame(ID = c(1,2,3), var1 = c(3,4,5), var2 = c(6,3,2)) B <- data.frame(ID = c(1,2,3), var1 = c(4,4,5), var2 = c(6,7,3)) C <- data.frame(ID = c(1,2,3), var1 = c(1,4,8), var2 = c(9,2,3)) list1 <- list(A = A, B = B, C = C) list2 <- list(A = A, B = B) combined <- map2(list1, list2, full_join, by = 'ID') This returns an error that the two lists are of different lengths. The only other way I have thought of

Joining dataframes from lists of unequal length

僤鯓⒐⒋嵵緔 提交于 2021-02-10 06:15:17
问题 I have two lists of dataframes that I want to combine, I have tried the following: A <- data.frame(ID = c(1,2,3), var1 = c(3,4,5), var2 = c(6,3,2)) B <- data.frame(ID = c(1,2,3), var1 = c(4,4,5), var2 = c(6,7,3)) C <- data.frame(ID = c(1,2,3), var1 = c(1,4,8), var2 = c(9,2,3)) list1 <- list(A = A, B = B, C = C) list2 <- list(A = A, B = B) combined <- map2(list1, list2, full_join, by = 'ID') This returns an error that the two lists are of different lengths. The only other way I have thought of

How to filter rows for every column independently using dplyr

丶灬走出姿态 提交于 2021-02-10 05:51:50
问题 I have the following tibble: library(tidyverse) df <- tibble::tribble( ~gene, ~colB, ~colC, "a", 1, 2, "b", 2, 3, "c", 3, 4, "d", 1, 1 ) df #> # A tibble: 4 x 3 #> gene colB colC #> <chr> <dbl> <dbl> #> 1 a 1 2 #> 2 b 2 3 #> 3 c 3 4 #> 4 d 1 1 What I want to do is to filter every columns after gene column for values greater or equal 2 (>=2). Resulting in this: gene, colB, colC a NA 2 b 2 3 c 3 4 How can I achieve that? The number of columns after genes actually is more than just 2. 回答1: One

How to filter rows for every column independently using dplyr

半腔热情 提交于 2021-02-10 05:50:06
问题 I have the following tibble: library(tidyverse) df <- tibble::tribble( ~gene, ~colB, ~colC, "a", 1, 2, "b", 2, 3, "c", 3, 4, "d", 1, 1 ) df #> # A tibble: 4 x 3 #> gene colB colC #> <chr> <dbl> <dbl> #> 1 a 1 2 #> 2 b 2 3 #> 3 c 3 4 #> 4 d 1 1 What I want to do is to filter every columns after gene column for values greater or equal 2 (>=2). Resulting in this: gene, colB, colC a NA 2 b 2 3 c 3 4 How can I achieve that? The number of columns after genes actually is more than just 2. 回答1: One

ggplot with two y-axis in R?

跟風遠走 提交于 2021-02-08 12:11:40
问题 I have asked this question earlier but someone close it indicating that it has answer. I am pretty confuse how i can get the two variables plotted on two Y-axis . I want to plot Level on the left y-axis and Flow on the right y-axis (ie., secondary axis). Here is my data and i would appreciate a response. library(tidyverse) library(lubridate) set.seed(1500) FakeData <- data.frame(Date = seq(as.Date("2020-01-01"), to = as.Date("2020-01-31"), by = "days"), Level = runif(31, 0, 30), Flow = runif

ggplot with two y-axis in R?

这一生的挚爱 提交于 2021-02-08 12:11:13
问题 I have asked this question earlier but someone close it indicating that it has answer. I am pretty confuse how i can get the two variables plotted on two Y-axis . I want to plot Level on the left y-axis and Flow on the right y-axis (ie., secondary axis). Here is my data and i would appreciate a response. library(tidyverse) library(lubridate) set.seed(1500) FakeData <- data.frame(Date = seq(as.Date("2020-01-01"), to = as.Date("2020-01-31"), by = "days"), Level = runif(31, 0, 30), Flow = runif

R pivot_wider to keep one id per row [duplicate]

佐手、 提交于 2021-02-08 12:02:14
问题 This question already has answers here : Transpose / reshape dataframe without “timevar” from long to wide format (8 answers) Closed 7 months ago . I have a dataset with IDs and Value where one ID could take multiple values. Currently, the same ID is repeated row-wise when it has multiple values, but I hope to keep one ID per row, adding more columns when necessary. Here's a reproducible example: df <- data.frame(id = c(1,1,1,2,3,3), val = c(10:15)) What I want is df2 <- data.frame(id = c(1:3