tidyverse

Reorganize data frame elements depending on the content of the rows in R

拈花ヽ惹草 提交于 2020-04-17 21:59:14
问题 I have this dataset: df <- structure(list(V1 = c("B1D01", "B1D01", "B1D01", "B1D01", "B1D01", "B1D01", "U0155"), V2 = c("U0155", "U0155", "U0155", "U0155", "U0155", "U0155", "U3003"), V3 = c("U3003", "U3003", "C1B00", "U3003", "U3003", "U3003", "C1B00"), V4 = c("C1B00", "C1B00", "U0073", "C1B00", "C1B00", "C1B00", "P037D"), V5 = c("P037D", "P037D", NA, "P037D", "P037D", "P037D", "P0616"), V6 = c("P0616", "P0616", NA, "P0616", "P0616", "P0616", "P0562"), V7 = c("P0562", "P0562", NA, "P0562",

Reorganize data frame elements depending on the content of the rows in R

女生的网名这么多〃 提交于 2020-04-17 21:59:07
问题 I have this dataset: df <- structure(list(V1 = c("B1D01", "B1D01", "B1D01", "B1D01", "B1D01", "B1D01", "U0155"), V2 = c("U0155", "U0155", "U0155", "U0155", "U0155", "U0155", "U3003"), V3 = c("U3003", "U3003", "C1B00", "U3003", "U3003", "U3003", "C1B00"), V4 = c("C1B00", "C1B00", "U0073", "C1B00", "C1B00", "C1B00", "P037D"), V5 = c("P037D", "P037D", NA, "P037D", "P037D", "P037D", "P0616"), V6 = c("P0616", "P0616", NA, "P0616", "P0616", "P0616", "P0562"), V7 = c("P0562", "P0562", NA, "P0562",

Unnest list and concatenate in R

给你一囗甜甜゛ 提交于 2020-04-16 02:08:12
问题 I wish to unnest (flatten?) and concatenate strings (comma separated) of text within a tibble . Example data: library(tidyverse) tibble(person = c("Alice", "Bob", "Mary"), score = list(c("Red", "Green", "Blue"), c("Orange", "Green", "Yellow"), "Blue")) # A tibble: 3 x 2 person score <chr> <list> 1 Alice <chr [3]> 2 Bob <chr [3]> 3 Mary <chr [1]> Expected output: tibble(person = c("Alice", "Bob", "Mary"), score = c("Red, Green, Blue", "Orange, Green, Yellow", "Blue" )) # A tibble: 3 x 2 person

Tunnel data-variables through env-variable with “{{” operator

夙愿已清 提交于 2020-04-11 08:52:11
问题 Following the example from https://www.tidyverse.org/blog/2020/02/glue-strings-and-tidy-eval/ How do you pass variables into this function? library(tidyverse) #> Warning: package 'tidyverse' was built under R version 3.6.2 #> Warning: package 'tidyr' was built under R version 3.6.2 mean_by <- function(data, by, var, prefix = "avg") { data %>% group_by({{ by }}) %>% summarise("{prefix}_{{ var }}" := mean({{ var }}, na.rm = TRUE)) } mean_by(mtcars, by = cyl, var = mpg, prefix = "avg") #> # A

How can I read multiple csvs and retain the number in the file name for each?

心已入冬 提交于 2020-04-10 06:00:12
问题 I have multiple csv files in a folder none of which have a header. I want to preserve the order set out by the number at the end of the file. The file names are "output-1.csv", "output-2.csv" and so on. Is there a way to include the file name of each csv so I know which data corresponds to which file. The answer [here][1] gets close to what I want. library(tidyverse) #' Load the data ---- mydata <- list.files(path = "C:\\Users\\Documents\\Manuscripts\\experiment1\\output", pattern = "*.csv")

Add rows in data frame if observations are missing [duplicate]

ε祈祈猫儿з 提交于 2020-03-25 18:47:05
问题 This question already has an answer here : How to complete missing factor levels in data frame? (1 answer) Closed 16 days ago . I have a df1 with multiple questionnaires (measure) per persons (id) which were answered at particular points in time (date). Normally every person should fill out three questionnaires per session ( first, pre, post ). Some participants fail to fill out all three questionnaires. They might only answer one or two of the three. Hence, the possible patterns could be

Web-Scraping in R programming (rvest)

痴心易碎 提交于 2020-02-23 06:28:29
问题 I am trying to scrape all details ( Type Of Traveller, Seat Type,Route,Date Flown, Seat Comfort, Cabin Staff Service, Food & Beverages, Inflight Entertainment,Ground Service,Wifi & Connectivity,Value For Money ) inclusive of the star rating from the airline quality webpage https://www.airlinequality.com/airline-reviews/emirates/ Not Working as expected my_url<- c("https://www.airlinequality.com/airline-reviews/emirates/") review <- function(url){ review<- read_html(url) %>% html_nodes("

Row to colnames

佐手、 提交于 2020-02-22 05:59:21
问题 Is there a quick way (part of the tidyverse API perhaps) to turn a row into column names for a data.frame or tibble , somewhat similar to tibble::column_to_rownames ? I realize there are many ways to do this, e.g. somewhat clumsily: > df <- head(iris) > > df %>% + set_colnames(magrittr::extract(., 1,)) %>% + magrittr::extract(-1,) 5.1 3.5 1.4 0.2 1 2 4.9 3.0 1.4 0.2 setosa 3 4.7 3.2 1.3 0.2 setosa 4 4.6 3.1 1.5 0.2 setosa 5 5.0 3.6 1.4 0.2 setosa 6 5.4 3.9 1.7 0.4 setosa 回答1: janitor::row_to

Row to colnames

守給你的承諾、 提交于 2020-02-22 05:57:02
问题 Is there a quick way (part of the tidyverse API perhaps) to turn a row into column names for a data.frame or tibble , somewhat similar to tibble::column_to_rownames ? I realize there are many ways to do this, e.g. somewhat clumsily: > df <- head(iris) > > df %>% + set_colnames(magrittr::extract(., 1,)) %>% + magrittr::extract(-1,) 5.1 3.5 1.4 0.2 1 2 4.9 3.0 1.4 0.2 setosa 3 4.7 3.2 1.3 0.2 setosa 4 4.6 3.1 1.5 0.2 setosa 5 5.0 3.6 1.4 0.2 setosa 6 5.4 3.9 1.7 0.4 setosa 回答1: janitor::row_to

Row to colnames

青春壹個敷衍的年華 提交于 2020-02-22 05:56:53
问题 Is there a quick way (part of the tidyverse API perhaps) to turn a row into column names for a data.frame or tibble , somewhat similar to tibble::column_to_rownames ? I realize there are many ways to do this, e.g. somewhat clumsily: > df <- head(iris) > > df %>% + set_colnames(magrittr::extract(., 1,)) %>% + magrittr::extract(-1,) 5.1 3.5 1.4 0.2 1 2 4.9 3.0 1.4 0.2 setosa 3 4.7 3.2 1.3 0.2 setosa 4 4.6 3.1 1.5 0.2 setosa 5 5.0 3.6 1.4 0.2 setosa 6 5.4 3.9 1.7 0.4 setosa 回答1: janitor::row_to