tidyr

pivot_wider does not seem to work with missing values. How to turn spread() into pivot_wider() when there is missing values

旧时模样 提交于 2021-02-11 15:28:06
问题 as the spread() function is being replaced by the new pivot_wider() function, I was trying to use the pivot_wider() from now on but it does not seem to work because of the missing values. Any help is much appreciated # This is an example I saw on the web surveys <- read.csv("http://kbroman.org/datacarp/portal_data_joined.csv", stringsAsFactors = FALSE) library(dplyr) surveys %>% filter(taxa == "Rodent", !is.na(weight)) %>% group_by(sex,genus) %>% summarize(mean_weight = mean(weight)) %>%

Expand nested dataframe into parent

时光总嘲笑我的痴心妄想 提交于 2021-02-10 15:10:22
问题 I have a dataframe nested within a dataframe that I'm getting from Mongo. The number of rows match in each so that when viewed it looks like a typical dataframe. My question, how do I expand the nested dataframe into the parent so that I can run dplyr selects? See the layout below 'data.frame': 10 obs. of 2 variables: $ _id : int 1551 1033 1061 1262 1032 1896 1080 1099 1679 1690 $ personalInfo:'data.frame': 10 obs. of 2 variables: ..$ FirstName :List of 10 .. ..$ : chr "Jack" .. ..$ : chr

Unused argument in summarise n() R

廉价感情. 提交于 2021-02-10 14:14:58
问题 I am trying to run the following code: DF2 %>% group_by(doy, yearadded) %>% summarise(n_entries= n(doy, yearadded)) Which gives me the error: Error in n(doy, yearadded) : unused arguments (doy, yearadded) My yearadded field is a character class and doy is a numeric, is that why it's not working or is there some other reason? 回答1: The n() doesn't take any arguments. It would be library(dplyr) DF2 %>% group_by(doy, yearadded) %>% summarise(n_entries= n()) Or more compactly count(DF2, doy,

converting multiple columns from wide to long using pivot_longer

我怕爱的太早我们不能终老 提交于 2021-02-08 11:47:19
问题 I get an error message when I want to convert multiple columns from wide to long with pivot_longer I have code which converts from wide to long with gather but I have to do this column by column. I want to use pivot_longer to gather multiple columns rather than column by column. This is some input data: structure(list(id = c("81", "83", "85", "88", "1", "2"), look_work = c("yes", "yes", "yes", "yes", "yes", "yes"), current_work = c("no", "yes", "no", "no", "no", "no"), before_work = c("no",

Combine dfs by common column importing selected columns in R

烈酒焚心 提交于 2021-02-08 05:31:37
问题 I would like to merge data.frames by common "names" column but only selecting the "PA" columns. df <- data.frame(names=c("Obs1", "Obs2", "Obs3", "Obs4", "Obs5"), `S1`=c(1,2,2,0,1), `S2`=c(2,50,40,30,22), `S3`=c( 0,100,135,256,303), `S4`=c(0,10,17,73,74),check.names=FALSE) df2<- data.frame(names=c("Obs1", "Obs2", "Obs3", "Obs4", "Obs5"), AB=c(0,30,30,40,2), PA=c(2,4,5,6,7)) df3<- data.frame(names=c("Obs1", "Obs2", "Obs3", "Obs4", "Obs5"), AB=c(100,300,300,400,200), PA=c(3,5,7,8,7)) df4<- data

Programming with tidyeval: The mutate function after tidyr::unite(col = !!col)

倾然丶 夕夏残阳落幕 提交于 2021-02-08 04:54:20
问题 So I want to make a function with unite() from tidyr, but it does not seem to work.. library(dplyr, warn.conflicts = FALSE) library(tidyr, warn.conflicts = FALSE) library(stringr, warn.conflicts = FALSE) mtcars %>% as_tibble() %>% select(mpg , cyl) %>% mutate_all(as.character) %>% unite(col = hello, sep = "/") %>% mutate(hello = str_replace(hello, "/", "")) #> # A tibble: 32 x 1 #> hello #> <chr> #> 1 216 #> 2 216 #> 3 22.84 #> 4 21.46 #> 5 18.78 #> 6 18.16 #> 7 14.38 #> 8 24.44 #> 9 22.84 #>

Programming with tidyeval: The mutate function after tidyr::unite(col = !!col)

白昼怎懂夜的黑 提交于 2021-02-08 04:54:09
问题 So I want to make a function with unite() from tidyr, but it does not seem to work.. library(dplyr, warn.conflicts = FALSE) library(tidyr, warn.conflicts = FALSE) library(stringr, warn.conflicts = FALSE) mtcars %>% as_tibble() %>% select(mpg , cyl) %>% mutate_all(as.character) %>% unite(col = hello, sep = "/") %>% mutate(hello = str_replace(hello, "/", "")) #> # A tibble: 32 x 1 #> hello #> <chr> #> 1 216 #> 2 216 #> 3 22.84 #> 4 21.46 #> 5 18.78 #> 6 18.16 #> 7 14.38 #> 8 24.44 #> 9 22.84 #>

Programming with tidyeval: The mutate function after tidyr::unite(col = !!col)

拈花ヽ惹草 提交于 2021-02-08 04:54:06
问题 So I want to make a function with unite() from tidyr, but it does not seem to work.. library(dplyr, warn.conflicts = FALSE) library(tidyr, warn.conflicts = FALSE) library(stringr, warn.conflicts = FALSE) mtcars %>% as_tibble() %>% select(mpg , cyl) %>% mutate_all(as.character) %>% unite(col = hello, sep = "/") %>% mutate(hello = str_replace(hello, "/", "")) #> # A tibble: 32 x 1 #> hello #> <chr> #> 1 216 #> 2 216 #> 3 22.84 #> 4 21.46 #> 5 18.78 #> 6 18.16 #> 7 14.38 #> 8 24.44 #> 9 22.84 #>

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

Reshape horizontal to to long format using pivot_longer

久未见 提交于 2021-02-05 08:09:10
问题 I am trying to reshape my data to long instead of wide format using the same code provided earlier link , however it doesn't work even after several trials to modify names_pattern = "(.*)_(pre|post.*)", My data sample is data1<-read.table(text=" Serial_ID pre_EDV pre_ESV pre_LVEF post_EDV post_ESV post_LVEF 1 76.2 32.9 56.8 86.3 36.6 57.6 2 65.4 35.9 45.1 60.1 26.1 56.7 3 64.4 35.1 45.5 72.5 41.1 43.3 4 50 13.9 72.1 46.4 18.4 60.4 5 89.6 32 64.3 70.9 19.3 72.8 6 62 20.6 66.7 55.9 17.8 68.2 7