rbind

Can you use rbind.fill without having it fill in NA's?

时光怂恿深爱的人放手 提交于 2019-12-11 07:39:06
问题 I am trying to combine two dataframes with different number of columns and column headers. However, after I combine them using rbind.fill() , the resulting file has filled the empty cells with NA . This is very inconvenient since one of the columns has data that is also represented as "NA" (for North America), so when I import it into a csv, the spreadsheet can't tell them apart. Is there a way for me to: Use the rbind.fill function without having it populate the empty cells with NA or Change

rbind data.frames without names

霸气de小男生 提交于 2019-12-10 12:56:32
问题 I am trying to figure out why the rbind function is not working as intended when joining data.frames without names. Here is my testing: test <- data.frame( id=rep(c("a","b"),each=3), time=rep(1:3,2), black=1:6, white=1:6, stringsAsFactors=FALSE ) # take some subsets with different names pt1 <- test[,c(1,2,3)] pt2 <- test[,c(1,2,4)] # method 1 - rename to same names - works names(pt2) <- names(pt1) rbind(pt1,pt2) # method 2 - works - even with duplicate names names(pt1) <- letters[c(1,1,1)]

assign row name while rbind a row in a data frame

空扰寡人 提交于 2019-12-10 12:24:23
问题 I want to assign a rowname (A_B) while I rbind a row into a new dataframe (d). The row is the result of the ratio of two rows of another data frame (df). df <- data.frame(ID = c("A", "B" ),replicate(3,sample(1:100,2,rep=TRUE))) d <- data.frame() d<-rbind(d, df[df$ID == "A",2:4 ]/df[df$ID == "B", 2:4]) Actual output X1 X2 X3 1 0.08 0.14 0.66 Expected output. Instead of rowname 1 I want A_B as result of A_B ratio X1 X2 X3 A_B 0.08 0.14 0.66 回答1: updated the solution to address multiple rows You

R and rbind making entries without the same length be zero

…衆ロ難τιáo~ 提交于 2019-12-09 06:50:20
问题 Say I have two vectors v1 and v2 and that I want to call rbind(v1, v2) . However, supposed length(v1) > length(v2) . From the documentation I have read that the shorter vector will be recycled. Here is an example of this "recycling": > v1 <- c(1, 2, 3, 4, 8, 5, 3, 11) > v2 <- c(9, 5, 2) > rbind(v1, v2) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] v1 1 2 3 4 8 5 3 11 v2 9 5 2 9 5 2 9 5 Is there any straightforward way I can stop v2 from being recycled and instead make the remaining entries 0? Is

sapply vs. lapply while reading files and rbind'ing them

痞子三分冷 提交于 2019-12-08 19:44:27
I followed Hadley's thread: Issue in Loading multiple .csv files into single dataframe in R using rbind to read multiple CSV files and then convert them to one dataframe. I also experimented with lapply vs. sapply as discussed on Grouping functions (tapply, by, aggregate) and the *apply family . Here's my first CSV file: dput(File1) structure(list(First.Name = structure(c(1L, 2L, 1L, 1L, 1L), .Label = c("A", "C"), class = "factor"), Last.Name = structure(c(1L, 2L, 2L, 2L, 2L), .Label = c("B", "D"), class = "factor"), Income = c(55L, 23L, 34L, 45L, 44L), Tax = c(23L, 21L, 22L, 24L, 25L),

R: rBind from Matrix package does not work for sparse matrices

北战南征 提交于 2019-12-08 09:12:19
问题 I have the following code: concept_vectors <- foreach(j = 1:2, .combine=rBind, .packages="Matrix") %do% { Matrix::colMeans(sparseX[1:10,],sparseResult=TRUE) } which results in the following error message: Error in { : no method for coercing this S4 class to a vector However, if I either remove 'sparseResult=TRUE' option, or do not use colMeans at all, the code works, even if without colMeans, sparseX is still an S4 object . If I replace rBind with rbind2 directly, then I still see the

sapply vs. lapply while reading files and rbind'ing them

天大地大妈咪最大 提交于 2019-12-08 06:09:19
问题 I followed Hadley's thread: Issue in Loading multiple .csv files into single dataframe in R using rbind to read multiple CSV files and then convert them to one dataframe. I also experimented with lapply vs. sapply as discussed on Grouping functions (tapply, by, aggregate) and the *apply family. Here's my first CSV file: dput(File1) structure(list(First.Name = structure(c(1L, 2L, 1L, 1L, 1L), .Label = c("A", "C"), class = "factor"), Last.Name = structure(c(1L, 2L, 2L, 2L, 2L), .Label = c("B",

Import and rbind multiple csv files with common name in R

为君一笑 提交于 2019-12-07 14:44:14
问题 I have multiple CSV files with 4 common character in their names. I want to know how I can rbind the files with the same common character. For example, "AM-25" is common in name of 3 csv files and "BA-35" in the name of another 2. The files are like this AM-25.myfiles.2000.csv, AM-25.myfiles.2001.csv ,AM-25.myfiles.2002.csv,BA-35.myfiles.2000.csv, BA-35.myfiles.2001.csv, I use this to read in all the files: files <- list.files(path=".", pattern="xyz+.csv", all.files = FALSE,full.names=TRUE )

Losing names of dimnames of a table after cbind or rbind

ぐ巨炮叔叔 提交于 2019-12-06 02:36:45
问题 After cbind or rbind -ing a table object (for example, adding a margin of sums or somesuch), names of dimnames get lost (see y ). I found this "workaround" but was wondering if there's an out of the bag solution to this that looks less hacky. Perhaps something that can be done on the fly? I would like to keep the object of class table . > (x <- table(1:3, sample(1:3), dnn = c("rows", "cols"))) cols rows 1 2 3 1 1 0 0 2 0 0 1 3 0 1 0 > (y <- cbind(x, "4" = 4:6)) # "rows" and "cols" get lost 1

How to avoid renaming of rows when using rbind inside do.call?

元气小坏坏 提交于 2019-12-05 21:28:10
问题 I am trying to bind some sub elements of the elements from the list The list OC is as follows > library(quantmod) > OC <- getOptionChain('AAPL', NULL) > str(OC) List of 9 $ Feb 2013:List of 3 ..$ calls :'data.frame': 35 obs. of 7 variables: .. ..$ Strike: num [1:35] 380 390 400 410 420 430 440 445 450 455 ... .. ..$ Last : num [1:35] 89.9 86 60 49.5 39.8 ... .. ..$ Chg : num [1:35] 0 0 -0.4 -4.4 -0.7 -1.9 -0.55 -0.7 -0.95 -1 ... .. ..$ Bid : num [1:35] 79.5 69.8 59.8 49.8 39.6 ... .. ..$ Ask