dplyr

Split Strings into values in long dataframe format [duplicate]

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-05 09:42:02
问题 This question already has answers here : Split comma-separated strings in a column into separate rows (6 answers) Split delimited strings in a column and insert as new rows [duplicate] (6 answers) Closed 3 years ago . I have a dataframe that looks like the following example df which consist of a character variable VAR . df<-data.frame(ID = 1:2, VAR = c("VAL1\r\nVAL2\r\nVAL8","VAL2\r\nVAL5"), stringsAsFactors = FALSE) # ID VAR # 1 1 VAL1\r\nVAL2\r\nVAL8 # 2 2 VAL2\r\nVAL5 I would like to split

Using pivot_longer to turn multiple columns into one in R [duplicate]

老子叫甜甜 提交于 2021-02-05 09:33:48
问题 This question already has an answer here : Using Reshape from wide to long in R [closed] (1 answer) Closed 6 months ago . I have a df population that looks something like this (not all columns and rows listed): Region X1975 X1976 X1977 ... X2008 National Total 942420 93717 94974 132802 Bejing 844.4 845.10 860.50 1695 Tianjin 702.86 706.50 712.87 968.87 Hebei 4913 4943 4998 6989 ... sum 91979 93275 94540 132058 difference 440 441 433 743 The columns go from X1975 to X2008 and have every year

R summarise by group sum giving NA

橙三吉。 提交于 2021-02-05 09:30:50
问题 I have a data frame like this Observations: 2,190,835 Variables: 13 $ patientid <int> 4489, 4489, 4489, 4489, 4489, 4489, 4489, 4489, 4489, 4489, 4489, 4489, 4489, 4489, 4489, 4489, 4489… $ preparationid <dbl> 1000307, 1000307, 1000307, 1000307, 1000307, 1000307, 1000307, 1000307, 1000307, 1000307, 1000307, 1… $ doseday <int> 90, 90, 91, 91, 92, 92, 92, 92, 93, 93, 93, 93, 94, 94, 94, 94, 95, 95, 95, 95, 99, 99, 100, 100, 10… $ route <fct> enteral., enteral., enteral., enteral., enteral.,

using mutate_at with the in operator %in%

安稳与你 提交于 2021-02-05 09:01:35
问题 I have a data frame with a few variables to reverse code. I have a separate vector that has all the variables to reverse code. I'd like to use mutate_at(), or some other tidy way, to reverse code them all in one line of code. Here's the dataset and the vector of items to reverse library(tidyverse) mock_data <- tibble(id = 1:5, item_1 = c(1, 5, 3, 5, 5), item_2 = c(4, 4, 4, 1, 1), item_3 = c(5, 5, 5, 5, 1)) reverse <- c("item_2", "item_3") Here's what I want it to look like with only items 2

using mutate_at with the in operator %in%

梦想与她 提交于 2021-02-05 09:01:34
问题 I have a data frame with a few variables to reverse code. I have a separate vector that has all the variables to reverse code. I'd like to use mutate_at(), or some other tidy way, to reverse code them all in one line of code. Here's the dataset and the vector of items to reverse library(tidyverse) mock_data <- tibble(id = 1:5, item_1 = c(1, 5, 3, 5, 5), item_2 = c(4, 4, 4, 1, 1), item_3 = c(5, 5, 5, 5, 1)) reverse <- c("item_2", "item_3") Here's what I want it to look like with only items 2

How to conditionally replace values with NA across multiple columns

北城余情 提交于 2021-02-05 08:45:21
问题 I would like to replace outliers in each column of a dataframe with NA. If for example we define outliers as being any value greater than 3 standard deviations from the mean I can achieve this per variable with the code below. Rather than specify each column individually I'd like to perform the same operation on all columns of df in one call. Any pointers on how to do this?! Thanks! library(dplyr) data("iris") df <- iris %>% select(Sepal.Length, Sepal.Width, Petal.Length)%>% head(10) # add a

Rbind list of vectors with differing lengths

邮差的信 提交于 2021-02-05 08:44:50
问题 I am new to R and I am trying to build a frequency/severity simulation. Everything is working fine except that it takes about 10min to do 10000 simulations for each of 700 locations. For the simulation of one individual location, I got a list of vectors with varying lengths and I would like to efficiently rbind these vectors, filling in NAs for all non-existing values. I would like R to return a data.frame to me. So far, I used rbind.fill.matrix after converting the vectors in the list to

Rbind list of vectors with differing lengths

怎甘沉沦 提交于 2021-02-05 08:44:26
问题 I am new to R and I am trying to build a frequency/severity simulation. Everything is working fine except that it takes about 10min to do 10000 simulations for each of 700 locations. For the simulation of one individual location, I got a list of vectors with varying lengths and I would like to efficiently rbind these vectors, filling in NAs for all non-existing values. I would like R to return a data.frame to me. So far, I used rbind.fill.matrix after converting the vectors in the list to

mutate two or more columns if case_when is used

为君一笑 提交于 2021-02-05 08:39:27
问题 I am trying to use the case_when function for a bunch of columns y a data.frame. This case does not return the specified columns in mutate cars %>% mutate ( km = speed * dist, mt = km / 1000 ) %>% mutate ( .funs = case_when( (speed < 20 ) ~ { km = km * 2 mt = mt * 3 } ) ) Thanks 回答1: We could use mutate_at library(tidyverse) cars %>% mutate(km = speed * dist, mt = km/1000) %>% mutate_at(vars(km, mt), funs(case_when(speed < 20 ~ .*2, TRUE ~ .))) If we need to do computation with separate

mutate two or more columns if case_when is used

纵饮孤独 提交于 2021-02-05 08:38:31
问题 I am trying to use the case_when function for a bunch of columns y a data.frame. This case does not return the specified columns in mutate cars %>% mutate ( km = speed * dist, mt = km / 1000 ) %>% mutate ( .funs = case_when( (speed < 20 ) ~ { km = km * 2 mt = mt * 3 } ) ) Thanks 回答1: We could use mutate_at library(tidyverse) cars %>% mutate(km = speed * dist, mt = km/1000) %>% mutate_at(vars(km, mt), funs(case_when(speed < 20 ~ .*2, TRUE ~ .))) If we need to do computation with separate