tidyr

Separate a column into 2 columns at the last underscore in R

人走茶凉 提交于 2019-12-19 18:13:12
问题 I have a dataframe like this id <-c("1","2","3") col <- c("CHB_len_SCM_max","CHB_brf_SCM_min","CHB_PROC_S_SV_mean") df <- data.frame(id,col) I want to create 2 columns by separating the "col" into the measurement and stat. stat is basically the text after the last underscore (max,min,mean, etc) My desired output is id Measurement stat 1 CHB_len_SCM max 2 CHB_brf_SCM min 3 CHB_PROC_S_SV mean I tried it this way but the stat column in empty. I am not sure if I am pointing to the last underscore

Separate a column into 2 columns at the last underscore in R

让人想犯罪 __ 提交于 2019-12-19 18:12:58
问题 I have a dataframe like this id <-c("1","2","3") col <- c("CHB_len_SCM_max","CHB_brf_SCM_min","CHB_PROC_S_SV_mean") df <- data.frame(id,col) I want to create 2 columns by separating the "col" into the measurement and stat. stat is basically the text after the last underscore (max,min,mean, etc) My desired output is id Measurement stat 1 CHB_len_SCM max 2 CHB_brf_SCM min 3 CHB_PROC_S_SV mean I tried it this way but the stat column in empty. I am not sure if I am pointing to the last underscore

Check if all values are numeric over multiple columns and convert them to numeric

我的未来我决定 提交于 2019-12-19 11:57:47
问题 I have a dataframe with all the columns being character like this. ID <- c("A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B") ToolID <- c("CCP_A","CCP_A","CCQ_A","CCQ_A","IOT_B","CCP_B","CCQ_B","IOT_B", "CCP_A","CCP_A","CCQ_A","CCQ_A","IOT_B","CCP_B","CCQ_B","IOT_B") Step <- c("Step_A","Step_A","Step_B","Step_C","Step_D","Step_D","Step_E","Step_F", "Step_A","Step_A","Step_B","Step_C","Step_D","Step_D","Step_E","Step_F") Measurement <- c("Length","Breadth","Width","Height",NA,NA

Fit a different model for each row of a list-columns data frame

落花浮王杯 提交于 2019-12-19 08:01:25
问题 What is the best way to fit different model formulae that vary by the row of a data frame with the list-columns data structure in tidyverse? In R for Data Science, Hadley presents a terrific example of how to use the list-columns data structure and fit many models easily (http://r4ds.had.co.nz/many-models.html#gapminder). I am trying to find a way to fit many models with slightly different formulae. In the below example adapted from his original example, what is the best way to fit a

Separating column using separate (tidyr) via dplyr on a first encountered digit

半腔热情 提交于 2019-12-19 05:13:00
问题 I'm trying to separate a rather messy column into two columns containing period and description . My data resembles the extract below: set.seed(1) dta <- data.frame(indicator=c("someindicator2001", "someindicator2011", "some text 20022008", "another indicator 2003"), values = runif(n = 4)) Desired results Desired results should look like that: indicator period values 1 someindicator 2001 0.2655087 2 someindicator 2011 0.3721239 3 some text 20022008 0.5728534 4 another indicator 2003 0.9082078

Comparison between dplyr::do / purrr::map, what advantages? [closed]

人走茶凉 提交于 2019-12-17 21:50:25
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 3 years ago . When using broom I was used to combine dplyr::group_by and dplyr::do to perform actions on grouped data thanks to @drob. For example, fitting a linear model to cars depending on their gear system: library("dplyr") library("tidyr") library("broom") # using do() mtcars %>%

Split character string multiple times every two characters

早过忘川 提交于 2019-12-17 20:37:26
问题 I have a character column in my dataframe that looks like df<- data.frame(a=c("AaBbCC","AABBCC","AAbbCC"))#df a 1 AaBbCC 2 AABBCC 3 AAbbCC I would like to split this column every two characters. So in this case I would like to obtain three columns named VA,VB,VC . I tried library(tidyr) library(dplyr) df<- data.frame(a=c("AaBbCC","AABBCC","AAbbCC"))%>% separate(a,c(paste("V",LETTERS[1:3],sep="")),sep=c(2,2)) VA VB VC 1 Aa BbCC 2 AA BBCC 3 AA bbCC but this is not the desired result. I like to

From long to wide data with multiple columns

倾然丶 夕夏残阳落幕 提交于 2019-12-17 20:12:49
问题 Suggestions for how to smoothly get from foo to foo2 (preferably with tidyr or reshape2 packages)? This is kind of like this question, but not exactly I think, because I don't want to auto-number columns, just widen multiple columns. It's also kind of like this question, but again, I don't think I want the columns to vary with a row value as in that answer. Or, a valid answer to this question is to convince me it's exactly like one of the others. The solution in the second question of "two

Unnest a list column directly into several columns

给你一囗甜甜゛ 提交于 2019-12-17 19:57:53
问题 Can I unnest a list column directly into n columns? The list can be assumed to regular, with all elements being of equal length. If instead of a list column I would have a character vector, I could tidyr::separate . I can tidyr::unnest , but we need another helper variable to be able to tidyr::spread . Am I missing an obvious method? Example data: library(tibble) df1 <- data_frame( gr = c('a', 'b', 'c'), values = list(1:2, 3:4, 5:6) ) # A tibble: 3 x 2 gr values <chr> <list> 1 a <int [2]> 2 b

Retain attributes when using gather from tidyr (attributes are not identical)

末鹿安然 提交于 2019-12-17 18:53:00
问题 I have a data frame that needs to be split into two tables to satisfy Codd's 3rd normal form. In a simple case, the original data frame looks something like this: library(lubridate) > (df <- data.frame(hh_id = 1:2, income = c(55000, 94000), bday_01 = ymd(c(20150309, 19890211)), bday_02 = ymd(c(19850911, 20000815)), gender_01 = factor(c("M", "F")), gender_02 = factor(c("F", "F")))) hh_id income bday_01 bday_02 gender_01 gender_02 1 1 55000 2015-03-09 1985-09-11 M F 2 2 94000 1989-02-11 2000-08