Questions tagged [tidyr]

tidyr is an R package by Hadley Wickham for cleaning and reshaping data, designed to use the magrittr pipe (%>%) so as to interact well with dplyr and similar pipeable packages which emphasize tidy data. tidyr is the successor to reshape2.

tidyr is an package developed by Hadley Wickham and many others for cleaning and reshaping data, designed to use the pipe (%>%) so as to interact well with and similar pipeable packages which emphasize tidy data. tidyr is the successor to .

Links:

4200 questions
1
vote
1 answer

converting long to wide with columns starting at zero

I have the following data county<-c(a,a,a,b,b,c) id<-c(1,2,3,4,5,6) data<-data.frame(county,id) I need to convert from long to wide and get the following…
nms
  • 11
  • 2
1
vote
2 answers

How do I convert multiple columns into one from Binary data?

My data has 3 surveys per year (for 10 years) where 1 represents presence and 0s present absence. The subset looks like this x <- structure(c(0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, …
KimChiya
  • 31
  • 5
1
vote
1 answer

Using the broom::tidy to add confidence intervals to linear models in nested data frame

I've been trying follow the approach set out by Hadley Wickham for running multiple models in a nested data frame as per https://r4ds.had.co.nz/many-models.html I've managed to write this code below to create the multiple linear models: #create…
RobA
  • 13
  • 2
1
vote
2 answers

Matrix from dataframe in R/Python

I have a dataframe like this: If the person with that id has that fruit the value is 1, else it is 0. ID column is a primary key. ID Apple Orange Pear Grapes E1 1 0 1 1 E2 0 0 1 0 E3 0 1 1 0 E4 1 1 0 0 E5 1 0 0 1 I want a output…
ella
  • 89
  • 1
  • 7
1
vote
3 answers

Creating multiple rows from a complex string in R

Example of string in question called gene_snps: "ultra_rare_variant_chr9:23143143_A/C_chr9:5322432_G/T_chr9:9840984342_T/C;chr9:5324234:G/T;chr9:324424_T/A" Desired…
tacrolimus
  • 500
  • 2
  • 12
1
vote
2 answers

How to reshape using R for multiple value columns across one gather column

I want to reshape my data from wide to long using specific columns but create multiple value columns. Pasting below an example: data <- read.table(header=T, text=' hhid villageid hh_1 hh_2 age_1 age_2 1 10 ab pq 10 …
1
vote
0 answers

Error in unique.default(x, nmax = nmax) : unique() applies only to vectors by converting daywise data (daily) to monthly data using R

# On Linke topic1<-c("Polizei", "Deutschland") library(dplyr) library(tidyr) library(stringr) tokens_Linke_topic1<-tokens_keep(tokens_Linke, pattern = topic1) dfm_Linke=dfm(tokens_Linke_topic1) dfm_Linke_topic1<-dfm_group(dfm_Linke, groups =…
1
vote
0 answers

How create any metric descriptive statistics by group for all variables in R

Suppose i have these data glucose= structure(list(GR = c(1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L), glucose.1 = c(5.5, 4.77, 5.52, 4.97, 4.4, 5.54, 4.85, 5.5, 5.5, 5.5, 5.09, 5.51, 5.5, 5.5, 5.58, 5.58, 4.65,…
psysky
  • 3,037
  • 5
  • 28
  • 64
1
vote
1 answer

Create a vector of years and weeks with ISO-8601 format in R

I would like to create a vector from one date to another displayed as YYYYWW which increments by week. It is important that the weeks are displayed in ISO-8601 standard format, here is a link for reference to the ISO-8601:…
chilifan
  • 157
  • 1
  • 12
1
vote
2 answers

How to do a 2-step wrangling and nesting using tidyr/dplyr, using %>% pipe only?

I need to accomplish a wrangling task with tidyr/dplyr as part of a %>% pipe. That is, without assigning data to helper objects. I have the following trb tibble as a given: library(tibble) trb <- tribble(~name, ~type, ~dat, "john",…
Emman
  • 3,695
  • 2
  • 20
  • 44
1
vote
2 answers

Converting rows into columns based on the values in the rows, in R

This is the table I am working with. I want to convert reservation into separate columns. I want it to be transformed into something like this. I have been trying to do this using reshape2 and dplyr's separate but I didn't find a solution.
1
vote
3 answers

Replace NULL value in nested tibble / dataframe

I have this tibble of tibbles: a = tibble(a = c(1:3)) df <- tibble::tibble(a1 = list(a,a), a2 = list(NULL,a)) # A tibble: 2 x 2 a1 a2 1 2
Maël
  • 45,206
  • 3
  • 29
  • 67
1
vote
2 answers

compare sets of columns in R dataframe and keep one value from each set of two columns

Basically, I have a large dataset with many different variables. The data is ordered in pairs (2019 and 2020) and for some variables for neither year data is available for some only 2019 and some only 2020. I would like the 2020 data to 'override'…
L Smeets
  • 888
  • 4
  • 17
1
vote
2 answers

Reformat a table in R by splitting a set of values in a column

How to reformat a table in R from: Fruit Location Apple US, UK Pear PL, DE, FR, AR Banana CO to: Fruit Location Apple US Apple UK Pear PL Pear DE Pear FR Pear AR Banana CO ... assuming it comes in a file TSV…
bud.dugong
  • 689
  • 7
  • 16
1
vote
2 answers

Get a standard deviation by group and subtract from mean column standard deviation in R

I have a toy df that is included below. For columns Grades and Age, I need to find the difference between mean standard deviation from the total standard deviation, by University. The values in the new table - which should have 4 rows and 2 columns…
1 2 3
99
100