i need a guidance in one of my Task : here i need to rbind [complete row] the every two columns where the total number of columns are varying[any even No of Columns] depending on the dataset Browse and uploaded by user. Similar to image i have uploaded along with the Text,Basically we can say we are stacking every two column one above other and creating a new data frame which has two column as shown in Figure , Thank you well in advance
Asked
Active
Viewed 1,596 times
2
-
If you are seeking the multiple column names in different rows, it would not be useful – akrun Nov 22 '16 at 02:15
-
@akrun Appreciate you answer Sir actually after this i will be merging with another data frame for further computation , for that Purpose i am approaching this way. i tried with column wise merging after every 2nd column the size of keeps on enlarging and in this case No of rows will be increased. – Nabi Shaikh Nov 22 '16 at 02:17
2 Answers
5
We split
the dataset by the character substring of the column names (remove the numbers with gsub
) into a list
, change the names
of the list
with setNames
and rbind
the list
elements to a single data.table
using rbindlist
and specifying the idcol
as 'Tag'
library(data.table)
lst <- split.default(df1, gsub("\\d+", "", names(df1)))
#or use
#lst <- split.default(df1, cumsum(rep(c(TRUE, FALSE), ncol(df1)/2)))
rbindlist(setNames(lst, seq_along(lst)), idcol="Tag")
data
set.seed(24)
df1 <- as.data.frame(matrix(rnorm(10*8), ncol=8,
dimnames = list(NULL, paste0(rep(LETTERS[1:4], each = 2), 0:1))))

akrun
- 874,273
- 37
- 540
- 662
-
1this Code worked for Me : lst <- split.default(df, cumsum(rep(c(TRUE, FALSE), ncol(df)/2))) c<-rbindlist(setNames(lst, seq_along(lst)), idcol="Tag") Appreciate your Answere sir ! – Nabi Shaikh Nov 22 '16 at 06:17
-
How can we cbind from DF-02 to DF-01 depending Tag as shown in above question but this time we need DF-02 to DF-01 .Thank you well in advance – Nabi Shaikh Dec 19 '16 at 17:21
-
@NabiShaikh Not sure I understand your question. Perhaps `rbindlist(setNames(lst[rev(names(lst))], rev(seq_along(lst))), idcol="Tag")` – akrun Dec 19 '16 at 17:32
-
sir , http://stackoverflow.com/questions/41228187/cbind-complete-rows-depending-on-tag-no-1-to-maxtag-no – Nabi Shaikh Dec 19 '16 at 17:43
-
How can we use same code for rbinding required no of column Let say first 4 column or 5 column or 6 column .Thank you well in advance.`lst <- split.default(df1, cumsum(rep(c(TRUE, FALSE), ncol(df1)/2)))` this is for 2 column rbinding similarly how can we do for any required number of column rbinding.basically I want to understand the code – Nabi Shaikh Apr 04 '17 at 04:46
-
-
i have used `lst <- split.default(df1, cumsum(rep(c(TRUE, FALSE), ncol(df1)/2)))` for combining every 2 column earlier But can we use the same code for bind 4 column ..Basically what i am Looking is i want to understand `cumsum(TRUE,FALSE)` because their is always a requirement where i need to rbind any no of column ,so requesting you to plz explain on this..Thnk you sir – Nabi Shaikh Apr 04 '17 at 05:41
-
1@NabiShaikh Try `split.default(df1, cumsum(rep(c(TRUE, FALSE, FALSE, FALSE), length.out = ncol(df1))))` – akrun Apr 04 '17 at 05:42
-
i am unable understand this `cumsum(rep(c(TRUE, FALSE, FALSE, FALSE)` can you plz explain on this `let say for example i have to combine every 5 column ` Basically i want understand Please explain it will be helpful especially `(TRUE, FALSE, FALSE, FALSE)` how you decide the sequence ? – Nabi Shaikh Apr 04 '17 at 06:14
5
This is a reshape
operation if you rename your columns to group appropriately:
names(df1) <- gsub("(.)(.)", "\\2.\\1", names(df1))
reshape(df1, direction="long", varying=TRUE, sep=".", timevar="Tag")
# Tag 0 1 id
#1.A A -0.545880758 -1.31690812 1
#2.A A 0.536585304 0.59826911 2
#3.A A 0.419623149 -0.76221437 3
#4.A A -0.583627199 -1.42909030 4
#5.A A 0.847460017 0.33224445 5
#6.A A 0.266021979 -0.46906069 6
#7.A A 0.444585270 -0.33498679 7
#8.A A -0.466495124 1.53625216 8
#9.A A -0.848370044 0.60999453 9
#10.A A 0.002311942 0.51633570 10
#1.B B -0.074308561 -0.03373792 1
#2.B B -0.605156946 -0.58542756 2
# ...
Using @akrun's df1

thelatemail
- 91,185
- 12
- 128
- 188
-
@akrun's , Here we have rbinded every 2 column `what if we want to rbind 4 Columns instead of 2 column` – Nabi Shaikh Apr 03 '17 at 12:00
-
@akrun Earlier what we have done is `rbinding 2 columns` and what `if we rbind after every four column` Thank you well in advance sir – Nabi Shaikh Apr 03 '17 at 12:16