Questions tagged [cbind]

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.

450 questions
7
votes
1 answer

Split list every n elements and cbind, then rbind slices

I would like to slice every n elements of list, cbind the slice, and then rbind the slices. I can do this with the code below (n = 10 elements, list is 30 elements long). I 'manually' select every 10 elements of the list and then cbind these 10…
nofunsally
  • 2,051
  • 6
  • 35
  • 53
6
votes
2 answers

Combining cbind and paste in linear model

I would like to know how can I come up with a lm formula syntax that would enable me to use paste together with cbind for multiple multivariate regression. Example In my model I have a set of variables, which corresponds to the primitive example…
Konrad
  • 17,740
  • 16
  • 106
  • 167
6
votes
1 answer

cbind with data table and data frame

I have a question regarding the characteristics of cbind in a data table and a data frame. If I am binding a data frame(df) and a data table(dt), then the class of the resultant object depends on the first parameter. example: >…
Anuj
  • 303
  • 1
  • 3
  • 13
6
votes
1 answer

cbind converting factor to numeric

Not sure why this is happening. I have a dataframe df2 with the variables below: EVTYPE TOTAL_FATALITIES TOTAL_INJURIES (fctr) (dbl) (dbl) 1 TORNADO 5633 91346 2 EXCESSIVE HEAT …
Glitch
  • 673
  • 8
  • 23
5
votes
4 answers

Binding dataframes of different length (no cbind, no merge)

I am trying to display multiple dataframes next to each other to compare certain entries. However, they have a different number of rows and I want each data frame to be in the exact same order. I tried to use cbind which did not work because of the…
Linda Espey
  • 145
  • 5
5
votes
3 answers

Replacement of plyr::cbind.fill in dplyr?

I apologize if this question is elementary, but I've been scouring the internet and I can't seem to find a simple solution. I currently have a list of R objects (named vectors or dataframes of 1 variable, I can work with either), and I want to join…
Tom
  • 65
  • 1
  • 5
5
votes
1 answer

Read and cbind second column of multiple files in directory

I have 94 tab delimited files, no header, in a single directory '/path/' with gene names in the first column and counts in the second column. There are 23000 rows. I would like to read all 94 files found in /path/ in to R and merge all of the 94…
rvann
  • 75
  • 1
  • 6
5
votes
3 answers

cbind specific columns from multiple data.tables efficiently

I have a list of data.tables that I need to cbind, however, I only need the last X columns. My data is structured as follows: DT.1 <- data.table(x=c(1,1), y = c("a","a"), v1 = c(1,2), v2 = c(3,4)) DT.2 <- data.table(x=c(1,1), y = c("a","a"), v3 =…
greyBag
  • 387
  • 3
  • 14
5
votes
2 answers

how to cbind the column generated in a loop in R

I have a for loop that gives me a column of data per run. I run the for loop in the range 0:4, so it gives me 5 columns. If I print it out, it is like column1, column2, ... I want to save all of the 5 columns together as a csv file. In this one…
cppython
  • 63
  • 1
  • 1
  • 4
5
votes
2 answers

How to create a data frame with numeric and character columns?

I'm trying to build a data frame consisting of three character variables and one numeric variable. When I run the following code, I get a four-column matrix, but the score variable is no longer numeric, and the scores are treated as…
user3614783
  • 821
  • 6
  • 12
  • 20
5
votes
1 answer

In R, when using named rows, can a sparse matrix column be added (concatenated) to another sparse matrix?

I have two sparse matrices, m1 and m2: > m1 <- Matrix(data=0,nrow=2, ncol=1, sparse=TRUE, dimnames=list(c("b","d"),NULL)) > m2 <- Matrix(data=0,nrow=2, ncol=1, sparse=TRUE, dimnames=list(c("a","b"),NULL)) > m1["b",1]<- 4 > m2["a",1]<- 5 > m1 2 x 1…
ayman
  • 1,341
  • 12
  • 22
5
votes
2 answers

cbind replaces String with number?

x = iris$Sepal.Width; y = iris$Species; m = cbind(x,y); the output of m is: x y [1,] 3.5 1 [2,] 3.0 1 [3,] 3.2 1 [4,] 3.1 1 [5,] 3.6 1 [6,] 3.9 1 but I want 'setosa', etc in column y instead of a number how can I do that? I…
user2071938
  • 2,055
  • 6
  • 28
  • 60
5
votes
3 answers

cbind items from multiple lists recursively

Given three (or n lists): one <- list(a=1:2,b="one") two <- list(a=2:3,b="two") three <- list(a=3:4,b="three") What would be a more efficient way of cbindind each list item across the n lists, to get this…
thelatemail
  • 91,185
  • 12
  • 128
  • 188
4
votes
3 answers

Why can't I add numbers in a dataframe created with cbind?

I’m looking for advice on how to subtract values from each other listed in two data frames. In the example below with the two data frames A and B, I would like to subtract the values of the 2nd columns from each other on the condition that the first…
Thomas Larsen
  • 155
  • 1
  • 6
4
votes
2 answers

Inserting NAs columns in specific positions of a data frame in R

I'm struggling to try to insert NAs columns in specific positions of a data frame. For instance, I have the dataset: dataset <- data.frame(c1 = 1:5, c2 = 2:6, c3 = 3:7, c4 = 4:8,…
1
2
3
29 30