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
3
votes
4 answers
cbind 2nd element of list of lists together
I have data like the biglist example below, which is a list of 2 lists of 2. I would like to cbind the second list from each list together like the result example below. Normally, I would try something like lapply(biglist,cbind) but I'm not sure…

user3476463
- 3,967
- 22
- 57
- 117
3
votes
3 answers
Converting Likert Data to Numeric Across A Data Frame
I have a dataset with 90 responses to Likert Items that I would like to convert to numeric values. It is structured like the example here:
q6 <- c("Daily", "Never", "Often", "Very Often", "Daily")
q7 <- c("Never", "Never", "Often", "Often",…

Craig Hamilton
- 35
- 1
- 7
3
votes
3 answers
Inverse to cbind() function R
Suppose I have a list of matrices:
matrix <- matrix(1:4, nrow = 2, ncol = 2)
list <- list(matrix, matrix, matrix)
And a matrix created by function cbind():
long.matrix <- do.call(cbind, list)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 3 1…

cure
- 425
- 2
- 12
3
votes
1 answer
cbind a vector of different length to a dataframe
I have a dataframe consisting of two samples. Only one sample has answered a questionnaire about state anxiety.
For this case, I have calculated a vector for somatic state anxiety with the following function "rowSums":
som_lp <-…

C.Meadow
- 33
- 1
- 4
3
votes
1 answer
Column-by-Column cbind of lm output
I'd like to do a column-by-column comparison of two lm outputs. Something like this except I'd like to see the estimates next to the estimates nest to the estimates, the Std Error next to the Std Error, etc.
x=runif(6);y=runif(6);z=runif(6)
…

Rik
- 1,870
- 3
- 22
- 35
3
votes
1 answer
Cbind/Rbind With Ifelse Condition
Here is the code that I am working with:
x <- c("Yes","No","No","Yes","Maybe")
y <- t(1:10)
z <- t(11:20)
rbind.data.frame(ifelse(x == "Yes",y,z))
This produces
X1L X12L X13L X4L X15L
1 1 12 13 4 15
The desired outcome is:
x
1 …

Adam Warner
- 1,334
- 2
- 14
- 30
3
votes
1 answer
Faster way to fill in missing columns in R data frame
Can any R experts provide a faster way to do the following? My code works, but it takes 1 minute to do a 30,000-[column] by 12-[row] data frame. Thanks!
sync.columns = function(old.data, new.colnames)
{
# Given a data frame and a…

mc at uga dot edu
- 31
- 3
2
votes
2 answers
Create a new column in first dataframe with transposed and repeated values from a row in another dataframe
I have two data frames where the data are connected via the "ID" label as follows:
test_frame1<- data.frame(
ID = c("A","B","C"),
ElementType1 = c(1,6,1),
ElementType2= c(4,4,5),
ElementType3 = c('',6,1),
Notes = c("Something…

Katie S
- 23
- 4
2
votes
1 answer
In loop over dataframe rows, how to append list to dataframe
In a for loop that loops over rows of a dataframe, I calculate results based on the values in the dataframe columns. How can I append the results (a list) into new columns in the dataframe? Example below is a dummy function, the real function is…

a11
- 3,122
- 4
- 27
- 66
2
votes
2 answers
Combining two dataframes with alternating column position
I have these two dataframes:
df1 <- tibble(b = 1:4,
a = 1:4,
c = 1:4)
df2 <- tibble(b_B = 1:4,
a_A = 1:4,
c_C = 1:4)
> df1
# A tibble: 4 x 3
b a c
1 1 1 1
2 …

TarJae
- 72,363
- 6
- 19
- 66
2
votes
2 answers
One-liner to concatenate two data frames with a distinguishing column?
I often find myself creating two similar data frames that I'd like to rbind together but keep track of which one each row came from with a distinguishing column. My typical motif has been
new_df <- rbind(
cbind(df1, id="A"),
cbind(df2,…

Dubukay
- 1,764
- 1
- 8
- 13
2
votes
2 answers
cbind specific columns in list (without dplyr package)
I have a list with approximately 50 (slots) - dataframes. Each and every element of this list (dataframe) has 10 columns, 3 of which correspond to "year", "month" and "day". How can I cbind those three columns into one (by creating new column), in…

Kon Ath
- 183
- 1
- 2
- 13
2
votes
1 answer
How to make a function return two columns of different types (R)?
So I have a function that applies simple arithmetic to three columns of numeric data of a dataframe (TypeChart) and binds it to the row names of the dataframe:
TypeCombo <- function(type1, type2, type3) {
cbind(row.names(TypeChart),…

moistnar
- 75
- 4
2
votes
4 answers
How to combine/concatenate two dataframes one after the other but not merging common columns in R?
Suppose there are two dataframes as follows with same column names and I want to combine/concatenate one after the other without merging the common columns. There is a way of assigning it columnwise like df1[3]<-df2[1] but would like to know if…

Aaryan
- 31
- 4
2
votes
1 answer
How to add new columns into a specific position in a dataframe depending on user's choice in Shiny?
I am trying to create a Shiny app where you can choose what columns you want to add. In order to select the columns I have used checkBoxGroupInput to be able to add all of them (or more than 1) if the user wants.
The original dataframe without the…

emr2
- 1,436
- 7
- 23