tidyr

R - tidyr - mutate and spread multiple columns

房东的猫 提交于 2020-08-05 06:12:38
问题 I have the following dataframe in R my_df_test <- data.frame(V1 = c(1,2,1), V2 = c("A","B","A"), V3 = c("S1", "S1", "S2"), V4 = c("x","x","x"), V5 = c("y","y","y"), V6 = c("A", "B", "C"), V7 = c("D","E","F")) my_df_test V1 V2 V3 V4 V5 V6 V7 1 1 A S1 x y A D 2 2 B S1 x y B E 3 1 A S2 x y C F Now I want to check if the combination of values in V1 and V2, occurs multiple times in the df. In my example my_df lines 1 and 3 have the same values '1 A' and '1 A'. If this happens, I want the following

Order of variable names pivot_wider

夙愿已清 提交于 2020-08-04 10:24:30
问题 How can you change the order of the variable names in pivot_wider to have he names_from before the values_from? Using the us_rent_income dataset: df <- us_rent_income %>% pivot_wider(names_from = NAME, values_from = c(estimate, moe)) This gives results like 'estimate_Alabama', how do we change the order of the variable so it is 'Alabama_estimate'? 回答1: The documentation for pivot_wider() states "If values_from contains multiple values, the value will be added to the front of the output column

Order of variable names pivot_wider

左心房为你撑大大i 提交于 2020-08-04 10:20:48
问题 How can you change the order of the variable names in pivot_wider to have he names_from before the values_from? Using the us_rent_income dataset: df <- us_rent_income %>% pivot_wider(names_from = NAME, values_from = c(estimate, moe)) This gives results like 'estimate_Alabama', how do we change the order of the variable so it is 'Alabama_estimate'? 回答1: The documentation for pivot_wider() states "If values_from contains multiple values, the value will be added to the front of the output column

crossing / mutate is slow, how could i optimize this call?

二次信任 提交于 2020-07-20 03:42:39
问题 i have this call distances <- crossing(nodes, gps_points) %>% mutate(dist = geosphere::distHaversine(cbind(lon, lat), cbind(rlon, rlat))) But it takes a long time to complete. Just a crossing function takes 0.36 sec. Whole Statement takes 2.587 sec. Is it possible to optimize this call? dput(head(nodes)) structure(list(ids = c(292376151, 1112377287, 6902561109, 5324247975, 1112377281, 7018492265), ids_igraph = c(128974, 128973, 128972, 128971, 128970, 128969), lon = c(11.831088, 11.830884, 11

crossing / mutate is slow, how could i optimize this call?

谁说胖子不能爱 提交于 2020-07-20 03:42:02
问题 i have this call distances <- crossing(nodes, gps_points) %>% mutate(dist = geosphere::distHaversine(cbind(lon, lat), cbind(rlon, rlat))) But it takes a long time to complete. Just a crossing function takes 0.36 sec. Whole Statement takes 2.587 sec. Is it possible to optimize this call? dput(head(nodes)) structure(list(ids = c(292376151, 1112377287, 6902561109, 5324247975, 1112377281, 7018492265), ids_igraph = c(128974, 128973, 128972, 128971, 128970, 128969), lon = c(11.831088, 11.830884, 11

Wide to long: multiple columns, two timepoints, two groups

别说谁变了你拦得住时间么 提交于 2020-07-18 08:06:09
问题 I have searched and found a number of examples, so far I have not been able to solve a problem in transforming my data from wide to long. Below is an example of the data: set.seed(12345) id = 1:100 age = sample(1:100, 100, replace=TRUE) group = sample(1:2, 100, replace=TRUE) t0_var1 = sample(1:300, 100, replace=TRUE) t2_var1 = sample(1:300, 100, replace=TRUE) t0_var2 = sample(1:600, 100, replace=TRUE) t2_var2 = sample(1:600, 100, replace=TRUE) t0_var3 = sample(1:700, 100, replace=TRUE) t2

How to add additional columns using tidyr group_by function in R?

房东的猫 提交于 2020-07-07 11:25:05
问题 This question is a follow up to my post from this answer. Data df1 <- structure(list(Date = c("6/24/2020", "6/24/2020", "6/24/2020", "6/24/2020", "6/25/2020", "6/25/2020"), Market = c("A", "A", "A", "A", "A", "A"), Salesman = c("MF", "RP", "RP", "FR", "MF", "MF"), Product = c("Apple", "Apple", "Banana", "Orange", "Apple", "Banana"), Quantity = c(20L, 15L, 20L, 20L, 10L, 15L), Price = c(1L, 1L, 2L, 3L, 1L, 1L), Cost = c(0.5, 0.5, 0.5, 0.5, 0.6, 0.6)), class = "data.frame", row.names = c("1",

How to add additional columns using tidyr group_by function in R?

有些话、适合烂在心里 提交于 2020-07-07 11:24:39
问题 This question is a follow up to my post from this answer. Data df1 <- structure(list(Date = c("6/24/2020", "6/24/2020", "6/24/2020", "6/24/2020", "6/25/2020", "6/25/2020"), Market = c("A", "A", "A", "A", "A", "A"), Salesman = c("MF", "RP", "RP", "FR", "MF", "MF"), Product = c("Apple", "Apple", "Banana", "Orange", "Apple", "Banana"), Quantity = c(20L, 15L, 20L, 20L, 10L, 15L), Price = c(1L, 1L, 2L, 3L, 1L, 1L), Cost = c(0.5, 0.5, 0.5, 0.5, 0.6, 0.6)), class = "data.frame", row.names = c("1",

combine two data frames with all posible combinations

♀尐吖头ヾ 提交于 2020-07-01 07:35:18
问题 I have 2 data frames. How I can make something like tidyr::complete with them using tidyverse ? My data: df <-data.frame(a=letters[1:2] ) df1<-data.frame(one=1:2) Expected Result: a 1 b 1 a 2 b 2 Thx! 回答1: With this particular example I think you can just use the merge function. As a standard its arguments all.x and all.y are set to TRUE, so it automatically creates all combinations since the dataframes do not have any variables or values in common. df <-data.frame(a=letters[1:10] ) df1<-data

How to install pivot_long() and pivot_wide() in R

半世苍凉 提交于 2020-06-27 13:06:12
问题 If you want to try these new functions ( pivot_wide and pivot long ), you need to install the development version of tidyr : devtools::install_github("tidyverse/tidyr") . But I have not managed to achieved it. I install a list of libraries except one, ( vctrs ) and I don't know if that's the problem. When I run the next code: mtcars_wide1 <- mtcars %>% pivot_wide(names_from = "am", values_from = "mpg") R couldn't find the function. enter image description here Can you recommend me something?