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
1
vote
1 answer
How to cbind unequal length columns in r, matching the length of the shortest and accounting for date differences?
Problem:
I am trying to combine the columns of two vectors with different lengths and starting dates (think stock prices) and want to cut off the excess from the longest while matching the length of the shortest. Any help would be appreciated!
What…

Dustin
- 11
- 1
1
vote
2 answers
cbind dataframes in loop
I have n number of dataframes named "s.dfx" where x=1:n. All the dataframes have 7 columns with different names. Now I want to cbind all the dataframes.
I know the comand
t<-cbind.data.frame(s.df1,s,df2,...,s.dfn)
But I want to optimize and cbind…

Nicolaj Hansen
- 69
- 1
- 9
1
vote
1 answer
How to name dataframes by permutation order of columns using first letter
From a single data.frame, I generated as many data.frames as permutation of columns in the main data.frame. From here, I would like 1) each permuted data.frame to be named after the permutation order by keeping the first letter of each column name,…

Elixterra
- 281
- 1
- 3
- 11
1
vote
0 answers
How to save the values of the factors of a CFA analysis in my dataset
I performed a CFA using the lavaan package
require('lavaan');
HS.model <- 'external_regulation_soc =~ JOBMOTIVATIE_extsoc1 +
JOBMOTIVATIE_extsoc2 + JOBMOTIVATIE_extsoc3
external_regulation_mat =~ JOBMOTIVATIE_extmat1…

Lies Henderickx
- 11
- 1
1
vote
2 answers
add rows to the shorter matrices in a list to match longest one in list
I have a list of matrices that I want to be able to cbind into one matrix, but I run into a problem when they have different sized rows. To fix this I am trying to add empty rows to the bottom of the shorter ones, however the second to last step…

Rafael
- 3,096
- 1
- 23
- 61
1
vote
1 answer
Combine all columns with a common reference name into one column in R (reproducible code)
I have a data frame of columns, all characters, that I would like to join into one column, delimited by a _ or -, not joining NA's. Now this would usually be very easy, BUT problem is I don't know how many columns there will be in the data frame.…

Neal Barsch
- 2,810
- 2
- 13
- 39
1
vote
1 answer
Difference between cbind with dataframe subset or indicating each column separately?
What is the difference between these two lines of codes?
varname1 <- cbind(df.name$var1, df.name$var2, df.name$var3)
varname2 <- cbind(df.name[1:3])
If I then try to use the next function I get an "invalid type (list) for variable "varname2".…

Afke
- 899
- 2
- 10
- 21
1
vote
1 answer
skip a parameter inside function if not provided
I have created a function to reshape (pivot) data, but the last part of the function is to cbind several columns into the df.
pivot_df <- function(df, new_col, col1, col2, col3) {
df_col_selected <- df[ , c(col1, col2, col3)]
…

Zmnako Awrahman
- 538
- 7
- 19
1
vote
2 answers
Merge Two data frames column by column. (creating a table out of two data frames)
I have two data frames of this format
df1=data.frame( Date = c(1,2,3,4,5), customer1 = c(6,7,8,4,2), customer2 =
c(2,3,6,5,3)... )
df2=data.frame( Date = c(1,2,3,4,5), order1 = c(0,1,3,0,1), order2 =
c(0,1,0,0,2).. )
i want a result that…

Vin.Y
- 13
- 4
1
vote
1 answer
How to call columns for cbind using column names stored in a vector (attempting to perform pairwise comparison of columns with manova in R)
I am trying to perform a pairwise manova analysis where I loop through all the possible pairs of my columns. I think this is best communicated with an example:
varList <- colnames(iris)
m1 <- manova(cbind(varList[1], varList[2]) ~ Species, data =…

James Wade
- 337
- 2
- 3
- 13
1
vote
1 answer
R: Formatting loop output - without repeated column names?
I use following script to output the results for serial correlation:
serial = function(x,y,z){
for (i in 1:4 ) {
table_serial <- data.frame(i,
serial.test(VAR(cbind(x,y),p=i,type="const"),lags.pt=4, type=z)$serial$statistic[[1]], …

Fanny
- 55
- 11
1
vote
2 answers
r - lapply, ifelse and trying to compare row vs previous row in one column
Forgive me in advance for trying to use my excel logic in R, but I can't seem to figure this out. In a function, given X I am trying to find out if the row prior to it has a greater value or not using simple logic. If it is, show in the new column…

frameworkgeek
- 203
- 3
- 13
1
vote
3 answers
Bind different length data frames by columns from bottom
I am looking for options (preferably using base R) for binding different length data frames starting from bottom. The remaining/missing rows should be NAd out. Example:
set.seed(1212)
a = as.data.frame(rnorm(1, 0, 1))
b = as.data.frame(rnorm(4, 0,…

AK88
- 2,946
- 2
- 12
- 31
1
vote
3 answers
R: How can I cbind specific columns of all data frames of a nested loop within the loop?
I am trying to combine the third column of several data frames, which are called and renamed in a nested for loop, within the same looping process.
# Sample Data
ecvec_msa6_1998=matrix( round(rnorm(200, 5,15)), ncol=4)
ecvec_msa6_1999=matrix(…

BeSeLuFri
- 623
- 1
- 5
- 21
1
vote
2 answers
Reshaping a "vector" from long to wide
I have a vector with rownames, so it can be considered a "matrix" with 2 columns (one for filename, one for Topic):
> res
Topic
jardine-1.docx.md 1
jardine-2.docx.md 1
jardine-a1.docx.md 1
jardine-a2.docx.md …

hongsy
- 1,498
- 1
- 27
- 39