FAQ Database Discussion Community


Customized aggregation function with more than 1 input in dcast

r,reshape2
Is there anyway to use aggregate function with more than one input in dcast, or other functions can do this? For example names(airquality) <- tolower(names(airquality)) aqm <- melt(airquality, id=c("month", "day"), na.rm=TRUE) dcast(aqm, month ~ variable, mean) How can I calculate with mean of "value" weighted by day? Thanks in advance....

R: Converting Column Values into Their Own Binary Encoded Columns

r,machine-learning,sparse-matrix,reshape2
I have a number of CSV files with columns such as gender, age, diagnosis, etc. Currently, they are coded as such: ID, gender, age, diagnosis 1, male, 42, asthma 1, male, 42, anxiety 2, male, 19, asthma 3, female, 23, diabetes 4, female, 61, diabetes 4, female, 61, copd The...

Assigning a visit number to rows in a data frame using R

r,reshape2
I'm working with a large database of bird occurrence and abundance observations in a data frame in R. We visited each survey point multiple times across multiple years and entered a row with a bird code for every bird we saw. We tell visits to the same point apart using...

dplyr recast - variable not found

r,dplyr,reshape2,tidyr
First, here is the data tbl_df (simplified) I am using : > mytbldf Source: local data frame [6 x 5] iso2c country year var1 var2 1 BI Burundi 2011 4.486265 6.693711 2 BI Burundi 2012 3.939242 5.330326 3 BI Burundi 2013 4.286439 5.747370 4 UG Uganda 2011 3.998849 10.025680 5...

reshape2 dcast without aggregation - problems with seq

r,reshape2
I try to reshape a dataset from this (mydata - snippet) sample species cell_nr biovol 1 41442bay_1 Mytilus sp. 6.22 1243.04 2 41502elba_1 Mytilus sp. 1.35 260.64 3 41502bay_3 Mytilus sp. 2.74 548.21 4 41443bay_2 M. edulis 599.14 114028.15 5 41411elba_2 M. edulis 5107.51 1021502.16 to this (result) sample variable...

Finding paired events with reshape

r,reshape,reshape2
I have a list of users and items that have been bought by them at certain times, and I want to generate a list of those pairs from the raw data. While I can and probably will write a small python script to do it, I have the nagging feeling...

Reshaping large dataset in R

r,reshape,reshape2
I am trying to reshape a large dataset and have a problem not getting the results in the right order as I want to. Here is what the data looks like: GeoFIPS GeoName IndustryID Description X2001 X2002 X2003 X2004 X2005 10180 Abilene, TX 21 Mining 96002 92407 127138 150449 202926...

String matching records to count all instances in a dataframe

r,reshape2,stringr,agrep
I am trying to extract all strings from rows in a dataframe that match certain criteria for example how many words are match 'corn' in each row. Here is the input. install.packages('stringr') library(stringr) dataset <- c("corn", "cornmeal", "corn on the cob", "meal") y<- c('corn',"corn","mean","meal") id<- c(1,2,3,4) dataset <- data.frame(id,dataset,y) id...

Transform and Count Difference of Unique Customers over Time in R

r,dplyr,reshape2
I've got a data frame in R that looks like the following: cust = c("A", "B", "C", "A", "B", "E", "A", "F", "A", "G") period = as.Date(c("2013/1/1", "2013/1/1", "2013/1/1", "2013/1/2", "2013/1/2", "2013/1/2", "2013/1/3", "2013/1/3", "2013/1/4", "2013/1/4")) df = data.frame(cust, period) I wanted to transform it in a way that I...

In R: get multiple rows by splitting a column using tidyr and reshape2

r,split,reshape2,tidyr
What is the most simpel way using tidyr or reshape2 to turn this data: data <- data.frame( A=c(1,2,3), B=c("b,g","g","b,g,q")) Into (e.g. make a row for each comma separated value in variable B): A B 1 1 b 2 1 g 3 2 g 4 3 b 5 3 g 6...

Convert a long format to wide

r,function,reshape,reshape2
My data frame looks like this: x <- data.frame(c("a","a","a","a","b","b","c","c","c","a", "a"), c(1,2,3,4,1,2,1,2,3, 1, 2)) names(x) <- c("id","nr") id nr 1 a 1 2 a 2 3 a 3 4 a 4 5 b 1 6 b 2 7 c 1 8 c 2 9 c 3 10 a 1 11 a...

Overriding the sum values for the VennDiagram package

r,reshape2,venn-diagram
I have a dataset that kind of looks like this: ID X Y Z 1 T1 10 0 10 2 T2 0 0 20 3 T3 10 10 40 4 T4 0 30 10 5 T5 0 10 0 ... I can melt down the data with reshape2 and throw...

Reshaping a data frame and setting flag variables

r,dataframes,reshape2
I want to reshape my data frame from the df1 to df2 as appears below: df1 <- ID TIME RATEALL CL V1 Q V2 1 0 0 2.4 10 6 20 1 1 2 0.6 10 6 25 2 0 0 3.0 15 7 30 2 5 3 3.0 16...

colsplit in r: separate one column into two

r,split,reshape2
I have a csv file ("sumCounts") loaded in to r which contains a column called "transcript". An example of a row in this column is show below: TR43890|c0_g1_i1 I want to split this column into two columns called "transcript" and "isoform" along the pipe "|" character. sumCounts <- colsplit(transcript, "|",...

data cleaning and matching names in r

r,matching,reshape,reshape2
df1 <- structure(list(Name1 = structure(c(1L, 2L, 2L, 2L, 2L, 2L, 3L, 4L, 4L, 4L, 5L, 5L, 6L, 6L, 6L, 6L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 11L, 12L, 12L, 13L, 14L, 14L, 14L, 15L, 15L, 16L, 16L, 17L, 17L, 17L, 18L, 19L, 19L,...

From long to wide form without id.var?

r,reshape2,melt
I have some data in long form that looks like this: dat1 = data.frame( id = rep(LETTERS[1:2], each=4), value = 1:8 ) In table form: id value A 1 A 2 A 3 A 4 B 5 B 6 B 7 B 8 And I want it to be in...

strsplit with non-character data

r,reshape,reshape2,strsplit,agrep
1I want to do a strsplit on one variable ID1 to split into ID1_s1 and ID1_s2 and I need to get rid of the strings that are in brackets. # dummy data df1 <- data.frame(ID1=c("Gindalinc","Xaviertechnolgies","anine.inc(Nasq)","Xyzinc"), y=1:4) strsplit(df1$ID1, "\\(") How can i do a strplit to separate the ID1 based into...

Transposing a data.table with reshape2:::dcast

r,data.table,transpose,reshape2
I am transposing a data.table and have chosen to use reshape2:::dcast, however I am plagued by a strange handling of the data.table... here is a toy data set that replicates the behavior: > library(data.table) > library(reshape2) > DT <- structure(list(STORE = c(32123L, 32469L, 33177L, 33484L, 34772L, 34875L), VOLUME = c(343.87205,...

transform data from rows to column in R

r,reshape2
I am facing problem in transforming data from columns to row or wide format. My dataframe look like as below V1 V2 V3 V4 1 1 2.041 NA 1 123 5.190 NA 1 366 9.500 NA 1 1462 16.800 NA 1 2558 23.500 88 2 1 2.466 NA 2 123...

reshape data with non-unique id and varying time frames

r,reshape,reshape2,melt
I have a dataset with the following format: name1 year name2 profits2010 profits2009 count AA 2009 AA 10 15 20 AA 2010 AA 10 15 3 BB 2009 BB 4 NA 34 BB 2010 BB 4 NA 4 I need to reshape the data to this format.Any ideas on how...

Make a frequency data frame from a list while maintaining rows in R

r,plyr,reshape2
I have a list that looks like this: >AP $CMP1 [1] 411050384 411050456 411050456 411058568 $CMP2 [1] 411050384 411050456 $CMP3 [1] 411050384 411050456 411058568 428909002 428909002 And I want to transform the list into a data frame that uses every unique entry as a column name, and the entries in...

Convert from n x m matrix to long matrix in R

r,matrix,reshape,reshape2
Note: This is not a graph question. I have an n x m matrix: > m = matrix(1:6,2,3) > m a b c d 1 2 3 e 4 5 6 I would like to convert this to a long matrix: > m.l a d 1 a e 4 b...

Transposing data frames

r,reshape2,tidyr
Happy Weekends. I've been trying to replicate the results from this blog post in R. I am looking for a method of transposing the data without using t, preferably using tidyr or reshape. In example below, metadata is obtained by transposing data. metadata <- data.frame(colnames(data), t(data[1:4, ]) ) colnames(metadata) <-...

reshape2 dcast - values in fun.aggregate tabulate

r,reshape2
I would like to tabulate a long data.frame. The following example code creates a long data.frame, with columns "ID", "unit" and "mat". Using dcast I would like to create a wide format data.frame with new columns for each "mat", and the values should be either 1 (this combination exists) or...

Converting columns into rows without specifying the column names

r,reshape2,tidyr
I have a data frame with following structure: bad_df <- data.frame( id = c("id001", "id002", "id003"), participant.1 = c("Jana", "Marina", "Vasilei"), participant.2 = c("Niko", "Micha", "Niko"), role.1 = c("writer", "writer", "speaker"), role.2 = c("observer", "observer", "observer"), stringsAsFactors = F ) bad_df I would need to gather it into something like...

How to use dcast function to transform my dataset

python,r,reshape2
I have a very big dataset. It consist more than 10 million records. It is very difficult to use this much of dataset to apply any algorithm. So, that I trying to restructure this dataset. In my dataset, so many records are there per one customer. Now I am trying...

R melt function ERROR: non-numeric argument to mathematical function

r,ggplot2,dataframes,reshape2
I have a data.frame "df" such as: df Time T1 T2 T3 2015-02-20 04:00:00.00 128.0 136.5 27.6 2015-02-20 04:00:00.25 128.1 136.3 27.7 2015-02-20 04:00:00.50 128.3 136.2 27.7 2015-02-20 04:00:00.75 128.5 136.1 27.7 2015-02-20 04:00:01.00 128.6 136.1 27.7 2015-02-20 04:00:01.25 129.0 135.7 27.7 2015-02-20 04:00:01.50 129.1 135.6 27.8 2015-02-20 04:00:01.75 129.3...

How to make reshape2::acast throw an error upon variable aggregation?

r,reshape2
> DF = data.frame(expand.grid(LETTERS[1:3], LETTERS[4:5])[-3,], value=1:5) > DFa = rbind(DF, c("A","D",6)) > DF Var1 Var2 value 1 A D 1 2 B D 2 4 A E 3 5 B E 4 6 C E 5 > DFa Var1 Var2 value 1 A D 1 2 B D 2 4...

Unstacking a data frame

stack,reshape2,melt
I have a data frame which looks like this: d <- c("a", "b", "c", "a", "b", "c", "a", "b", "c") par <- c("a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9") df1 <- as.data.frame(cbind(id, par)) I would like it to look like this: a <- c("a1", "a4", "a7") b <-...