The functions cbind and rbind are S3 generic, with methods for data frames. The data frame method will be used if at least one argument is a data frame and the rest are vectors or matrices. There can be other methods; in particular, there is one for time series objects. See the section on ‘Dispatch’ for how the method to be used is selected.
Questions tagged [cbind]
450 questions
2
votes
1 answer
R write function to bind column to dataframe
I wrote a function which creates a vector of values via a substring operation on a column of an existing data frame:
fetchParent <- function(column){
substr(column,1,regexpr(":", column)-1)
}
An example run of this function…

Charles Knell
- 117
- 1
- 9
2
votes
1 answer
neutral element to cbind with dataframe
i can do the following:
a <- rep(5,5)
> cbind(6,a)
a
[1,] 6 5
[2,] 6 5
[3,] 6 5
[4,] 6 5
[5,] 6 5
> cbind(NULL,a)
a
[1,] 5
[2,] 5
[3,] 5
[4,] 5
[5,] 5
When i do it with a dataframe it gives me an error
> cbind(NULL,mtcars)
Error in…

Andre Elrico
- 10,956
- 6
- 50
- 69
2
votes
2 answers
Combining lists of different lengths into data frame
I have data like the SampleData below, which has lists of different length that I'd like to combine in to a data frame like the Desired Result below. I've tried using lapply and cbind.na from the qpcR package like the example below, but for some…

user3476463
- 3,967
- 22
- 57
- 117
2
votes
2 answers
Combine two identical dataframe columns into comma seperated columns in R
I have two identically structured dataframe (same amount of rows, columns and same headers). What I would like to do is to combine the two into one dataframe that has comma seperated columns.
I know how to do it with this dummy data frames, but…

T. BruceLee
- 501
- 4
- 16
2
votes
4 answers
R: merge columns from same data.frame based on NA positions
I have a dataframe like this:
df <- data.frame(theme1=c("hello",NA,NA,NA), theme2=c(NA,"world",NA,NA), theme3=c(NA,NA,"good_morning",NA), theme4=c(NA,NA,NA,"good_evening"))
theme1 theme2 theme3 theme4
1 hello NA NA …

Sander Van der Zeeuw
- 1,092
- 1
- 13
- 35
2
votes
2 answers
Fast concatenation of thousands of files by columns
I am using R to cbind about ~11000 files using:
dat <- do.call('bind_cols',lapply(lfiles,read.delim))
which is unbelievably slow. I am using R because my downstream processing like creating plots etc is in R. What are some fast alternatives to…

Komal Rathi
- 4,164
- 13
- 60
- 98
2
votes
2 answers
array in R number of items to replace is not a multiple of replacement length
I would like to ask, how to set a value in array using looping. say like this
a<-3
b<-4
for( i in 1:5)
{
x[i] <- cbind(a*i, b*i)
}
but i always get error saying : In x[i] <- cbind(a * i, b * i) :
number of items to replace is not a multiple of…

Elbert
- 516
- 1
- 5
- 15
2
votes
2 answers
Sum count data in a dataframe based on the size of an associated numeric variable
I have a data frame with data as follows (although my data set is much bigger)
ID Count Size
1 1 35
1 2 42
1 2 56
2 3 25
2 5 52
2 2 62
etc....
I would like to extract the total count for each ID but split…

J. Cee
- 49
- 5
2
votes
1 answer
merging matrices with cbind and match: name of the last column is dropped
I have a list of matrices:
[[1]]
a b
X2005.06 NA 2504179
X2006.06 NA 3746905
X2007.06 NA 5468607
X2008.06 NA 6664545
X2009.06 NA 7339310
X2010.06 4602635 5587625
X2011.06 5188408 6498543
X2012.06 5190519…

Eldar Agalarov
- 4,849
- 4
- 30
- 38
2
votes
1 answer
Create a character vector column of predefined text and bind it to existing dataframe using rbind or bind_rows
Good day,
I will present two [likely] very puny problems for your excellent review.
Problem #1
I have a relatively tidy df (dat) with dim 10299 x 563. The 563 variables common to both datasets [that created] dat are 'subject' (numeric), 'label'…

Zach
- 1,316
- 2
- 14
- 21
2
votes
2 answers
Dumb rbind for data.frames of different length
I don't need any smart rbind, like rbindlist, rbind.fill, bind_row and other.
I need a dumb rbind to simply bind two dataframes:
> a <- data.frame(a = 1:3)
> b <- data.frame(b = 1:2)
> some.magic.bind(a, b) # what function to use here?
a b
1 …

m0nhawk
- 22,980
- 9
- 45
- 73
2
votes
1 answer
How to retain column headings when using cbind() with matrices in R
Say one is trying to paste two matrices together, both of which have been given column labels using a list() with colnames(). Using cbind() in R works as expected for the data, but the column labels seem to get lost after the cbind() operation (the…

Dennis
- 51
- 1
- 4
2
votes
1 answer
Using cbind and naming time series matrices
When the cbind function is used to combine 2 or more matrices, the resulting matrix inherits the column names. An easy example of this fact is the following. I have two (2x2) matrices m1 and m2. The columns of m1 are a and b; the columns of m2 are c…

SavedByJESUS
- 3,262
- 4
- 32
- 47
2
votes
2 answers
How to add new column depends on row value in R?
I am trying to add a new column (slot) to a data.frame, depending on the row value of id. The short representative data is as following.
id Cap Rs R_inv
257 464A485SSEE3 1.41E-10 736665.125 1.36E-06
…

Rick
- 229
- 1
- 3
- 11
2
votes
0 answers
read a selected column from multiple csv files and combine into one large file in R
Hi,
My task is to read selected columns from over 100 same formatted .csv files in a folder, and to cbind into a big large file using R. I have attached a screen shot in this question for a sample data file.
This is the code I'm using:
…

Vicki1227
- 489
- 4
- 6
- 19