tidyr

Reorganizing columns by two column combination [duplicate]

心不动则不痛 提交于 2019-12-13 03:47:12
问题 This question already has answers here : merge list elements which are different dimensions r [duplicate] (2 answers) Closed 6 months ago . I am currently learning the tidyr and dplyr. Went in the following issue I am not sure how to appropiatly face: Imaging the following dataset: Factor 1 Factor 2 Year value A green 2016 1.2 A green 2017 1.9 B yellow 2017 3 B yellow 2018 8 An trying to obtain: Factor 1 Factor 2 Year.2016 Year.2017 Year.2018 A green 1.2 1.9 NA B yellow NA 3 8 I have basic R

How to transform R data from long-ish to wide-ish [duplicate]

喜你入骨 提交于 2019-12-13 02:58:10
问题 This question already has answers here : Convert data from long format to wide format with multiple measure columns (5 answers) Closed 23 days ago . I am using R to try to transform my data frame from "long-ish" to "wide-ish" and I have searched in vain for an answer that uses data similar in structure to mine. Here are my data: | ID | NAME | V1 | V2 | V3 | |------|------|-------|----:|-----:| | 1001 | Bob | Red | 302 | 0.50 | | 1001 | Bob | Blue | 737 | 0.50 | | 1002 | Jim | Red | 432 | 0.14

Removing incomplete cases from output of tidyr - gather() - r

ⅰ亾dé卋堺 提交于 2019-12-13 02:06:37
问题 I have untidy data in a dataframe that looks like this. Here you can see in 'team' the names of some soccer teams. Name1-3 are variable listing the different names used to refer to these teams in the first column. team name1 name2 name3 1 Loughborough Loughborough 2 Luton Town Luton Town Luton 3 Macclesfield Macclesfield 4 Maidstone United Maidstone United 5 Manchester City Manchester City Man City 6 Manchester United Manchester United Newton Heath Man United 7 Mansfield Town Mansfield Town

R: Spread key-value pairs when keys are in different columns and return value frequency

别来无恙 提交于 2019-12-13 00:24:03
问题 I have searched around but could not find a particular answer to my question. Suppose I have a data frame df: df = data.frame(id = c(10, 11, 12, 13, 14), V1 = c('blue', 'blue', 'blue', NA, NA), V2 = c('blue', 'yellow', NA, 'yellow', 'green'), V3 = c('yellow', NA, NA, NA, 'blue')) I want to use the values of V1-V3 as unique column headers and I want the occurrence frequency of each of those per row to populate the rows. Desired output: desired = data.frame(id = c(10, 11, 12, 13, 14), blue = c

R: Cleaning up a wide and untidy dataframe

☆樱花仙子☆ 提交于 2019-12-12 19:16:11
问题 I have a data frame that looks like: d<-data.frame(id=(1:9), grp_id=(c(rep(1,3), rep(2,3), rep(3,3))), a=rep(NA, 9), b=c("No", rep(NA, 3), "Yes", rep(NA, 4)), c=c(rep(NA,2), "No", rep(NA,6)), d=c(rep(NA,3), "Yes", rep(NA,2), "No", rep(NA,2)), e=c(rep(NA, 7), "No", NA), f=c(NA, "No", rep(NA,3), "No", rep(NA,2), "No")) >d id grp_id a b c d e f 1 1 1 NA No <NA> <NA> <NA> <NA> 2 2 1 NA <NA> <NA> <NA> <NA> No 3 3 1 NA <NA> No <NA> <NA> <NA> 4 4 2 NA <NA> <NA> Yes <NA> <NA> 5 5 2 NA Yes <NA> <NA>

expand a data frame to have as many rows as range of two columns in original row [duplicate]

試著忘記壹切 提交于 2019-12-12 19:13:34
问题 This question already has answers here : Expand ranges defined by “from” and “to” columns (8 answers) Closed 2 years ago . I have a data frame as follows: structure(list(symbol = c("u", "n", "v", "i", "a"), start = c(9L, 6L, 10L, 8L, 7L), end = c(14L, 15L, 12L, 13L, 11L)), .Names = c("symbol", "start", "end"), class = "data.frame", row.names = c("1", "2", "3", "4", "5")) I want to as many rows as there are values in the range of (start, end) for each symbol. So, the final data frame will look

Split column of comma-separated numbers into multiple columns based on value

江枫思渺然 提交于 2019-12-12 18:28:19
问题 I have a column f in my dataframe that I would like to spread into multiple columns based on the values in that column. For example: df <- structure(list(f = c(NA, "18,17,10", "12,8", "17,11,6", "18", "12", "12", NA, "17,11", "12")), .Names = "f", row.names = c(NA, 10L), class = "data.frame") df # f # 1 <NA> # 2 18,17,10 # 3 12,8 # 4 17,11,6 # 5 18 # 6 12 # 7 12 # 8 <NA> # 9 17,11 # 10 12 How would I split column f into multiple columns indicating the numbers in the row. I'm interested in

Loop through each column and row, do stuff

混江龙づ霸主 提交于 2019-12-12 15:33:07
问题 I think this is the best way to describe what I want to do: df$column <- ifelse(is.na(df$column) == TRUE, 0, 1) But where column is dynamic. This is because I have about 45 columns all with the same kind of content, and all I want to do is check each cell, replace it with a 1 if there's something in it, a 0 if not. I have of course tried many different things, but since there seems to be no df[index][column] in R, I'm lost. I'd have expected something like this to work, but nope: for (index

How to tidy data from different columns into one [duplicate]

我只是一个虾纸丫 提交于 2019-12-12 12:29:15
问题 This question already has answers here : Reshaping multiple sets of measurement columns (wide format) into single columns (long format) (7 answers) Closed 2 years ago . I'm worried that my title does not exactly capture my question. Let me explain my issue as best as I can. This is part of my data. pdpass begyr1 gvkey1 endyr1 begyr2 gvkey2 endyr2 begyr3 gvkey3 endyr3 begyr4 gvkey4 endyr4 begyr5 gvkey5 endyr5 -16348 1991 26005 1993 1994 8852 1996 1997 3708 2005 NA NA NA NA NA NA -16179 2006

Another way to do pivot table in R

孤街醉人 提交于 2019-12-12 11:06:12
问题 I have data set like below: > head(worldcup) Team Position Time Shots Passes Tackles Saves Abdoun Algeria Midfielder 16 0 6 0 0 Abe Japan Midfielder 351 0 101 14 0 Abidal France Defender 180 0 91 6 0 Abou Diaby France Midfielder 270 1 111 5 0 Aboubakar Cameroon Forward 46 2 16 0 0 Abreu Uruguay Forward 72 0 15 0 0 Then there is a code count mean of certain variables: wc_3 <- worldcup %>% select(Time, Passes, Tackles, Saves) %>% summarize(Time = mean(Time), Passes = mean(Passes), Tackles =