rbind

Convert data frame of N columns into a data frame of two 'stacked' columns

纵饮孤独 提交于 2019-12-01 20:30:08
Hello Stack Community. I am doing work with network analytics and have a data reshaping question. My original data comes in as a series of columns each column being a "source" and "target" pair. The final data frame needs to be made up of two columns "source" and "target". Note these pairs are staggered as they source and targets are linked as in a directed network. (See the final_output in the code example for desired output) I created a very hacky method producing the output I need (see code below) but it does not accommodate differing numbers of columns without me adding variables and

Why does rbindlist not respect column names?

跟風遠走 提交于 2019-12-01 17:46:26
I just discovered this bug, only to find that some people are calling it a "feature" . This makes rbindlist NOT like do.call("rbind",l) as rbind WILL respect column names. Further, there is no mention of this entirely unexpected behavior in the documentation. Is this really intentional? Code example: > library(data.table) > DT1 <- data.table(a=1, b=2) > DT2 <- data.table(b=3, a=4) > DT1 a b 1: 1 2 > DT2 b a 1: 3 4 I would expect that rbind 'ing these would produce columns with a = 1,4 ; b = 2,3. And get that with rbind.data.table and rbind.data.frame , though rbind.data.table produces warnings

Create a variable that identifies the original data.frame after rbind command in R

倖福魔咒の 提交于 2019-12-01 05:17:18
I am relatively new to R and I would like to know how can I create a variable (number sequence) that identifies the each of the original data.frames before being joined with the rbind command. Since in the original data frames there is one variable that is a row ID number, if creating a loop that assigns a new number in the new variable each time it encounters the number 1 in the row ID, it should work... Thanks. There's a function in the gdata package called combine that does just that. df1 <- data.frame(a = seq(1, 5, by = 1), b = seq(21, 25, by = 1)) df2 <- data.frame(a = seq(6, 10, by = 1),

rbind data frames based on a common pattern in data frame name

半城伤御伤魂 提交于 2019-11-30 22:43:35
Say I have multiple data frames which all have identical vector names and I'd like to cbind all which have a commmon pattern. So for these 3 data frames: df.1 <- data.frame(column1 = factor(sample(c("Male","Female"), 10, replace=TRUE)), speed=runif(10)) df.2 <- data.frame(column1 = factor(sample(c("Male","Female"), 10, replace=TRUE)), speed=runif(10)) df.3 <- data.frame(column1 = factor(sample(c("Male","Female"), 10, replace=TRUE)), speed = runif(10)) I would like to rbind everything with the common pattern "df.*" I have tried creating a list and then creating a data-frame from this using:

how to determine if a character vector is a valid numeric or integer vector

☆樱花仙子☆ 提交于 2019-11-30 17:19:52
I am trying to turn a nested list structure into a dataframe. The list looks similar to the following (it is serialized data from parsed JSON read in using the httr package). myList <- list(object1 = list(w=1, x=list(y=0.1, z="cat")), object2 = list(w=NULL, x=list(z="dog"))) EDIT: my original example data was too simple. The actual data are ragged, meaning that not all variables exist for every object, and some of the list elements are NULL. I edited the data to reflect this. unlist(myList) does a great job of recursively flattening the list, and I can then use lapply to flatten all the

How to bind data.table without increasing the memory consumption?

橙三吉。 提交于 2019-11-29 15:22:18
I have few huge datatable dt_1, dt_2, ..., dt_N with same cols. I want to bind them together into a single datatable . If I use dt <- rbind(dt_1, dt_2, ..., dt_N) or dt <- rbindlist(list(dt_1, dt_2, ..., dt_N)) then the memory usage is approximately double the amount needed for dt_1,dt_2,...,dt_N . Is there a way to bind them wihout increasing the memory consumption significantly? Note that I do not need dt_1, dt_2, ..., dt_N once they are combined together. Other approach, using a temporary file to 'bind': nobs=10000 d1 <- d2 <- d3 <- data.table(a=rnorm(nobs),b=rnorm(nobs)) ll<-c('d1','d2',

Print a multiplication table with minimal code

风流意气都作罢 提交于 2019-11-29 12:43:59
In R, what is the fastest way(shortest code) to print multiplication table? The functions seq rep and the bind functions help, but I'm looking for the shortest line(s) of code to do this. rbind("1\'s"=1:12, "2\'s"=seq(2,24,2), "3\'s"=seq(3,36,3), "4\'s"=seq(4,48,4), "5\'s"=seq(5,60,5), "6\'s"=seq(6,72,6)) Prints the 1's through 6's going across (horizontally). Anyone know how to perform this in a more compact way? tbl <- outer(1:6, 1:12, "*") rownames(tbl) <- paste(1:6, "'s", sep="") tbl You could make slightly more compact by using paste0(1:6, "'s") This seems a slight improvement: > v<

Mass rbind.fill for many data frames

99封情书 提交于 2019-11-29 11:57:20
I am attempting to row bind many data frames together into a single massive data frame. The data frames are named sequentially with the first named df1 , the second named df2 , the third named df3 , etc. Currently, I have bound these data frames together by explicitly typing the names of the data frames; however, for a very large number of data frames (roughly 10,000 total data frames are expected) this is suboptimal. Here is a working example: # Load required packages library(plyr) # Generate 100 example data frames for(i in 1:100){ assign(paste0('df', i), data.frame(x = rep(1:100), y = seq

How to rbind all the data.frames in your working environment?

六眼飞鱼酱① 提交于 2019-11-29 09:58:53
I have over 50 data.frames in my working environment that I would like to rbind . Is there a way to rbind the data.frames with out having to type out each date.frame ? Example of what I have been doing: df <- rbind(A, B, C, D, E, F) I have tried: df <- rbind(ls()) But this just creates a list of names of all the data.frames in my working environment. You can search for objects of data.frame class, and use function mget to retrieve them. a = b = c = data.frame(x=1:2, y=3, z=1:4) d = "junk" e = list(poo="pah") ls() # [1] "a" "b" "c" "d" "e" dfs = sapply(.GlobalEnv, is.data.frame) dfs # a b c d e

Add a new row in specific place in a dataframe

拜拜、爱过 提交于 2019-11-29 03:00:07
问题 Heres my data: > data Manufacturers Models 1 Audi RS5 2 BMW M3 3 Cadillac CTS-V 4 Lexus ISF I would like to add 1 row in the fourth row, like this: > data Manufacturers Models 1 Audi RS5 2 BMW M3 3 Cadillac CTS-V 4 Benz C63 5 Lexus ISF I have tried to use the rbind() like this: Benz = data.frame(Manufacturers = "Benz", Models = "C63") newdata = rbind(data,Benz) But I cannot add to the place I want. I would appreciate any help on this question. Thanks a lot. 回答1: In case you don't want the