rbind

How do I understand the warnings from rbind?

血红的双手。 提交于 2020-01-14 14:38:54
问题 If I have two data.frames with the same column names, I can use rbind to make a single data frame. However, if I have one is a factor and the other is an int, I get a warning like this: Warning message: In [<-.factor ( *tmp* , ri, value = c(1L, 1L, 0L, 0L, 0L, 1L, 1L, : invalid factor level, NA generated The following is a simplification of the problem: t1 <- structure(list(test = structure(c(1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L), .Label = c("False", "True"), class = "factor")), .Names =

Convert a list of sf objects into one sf

妖精的绣舞 提交于 2020-01-13 02:28:13
问题 I have a list of sf objects that I would like to row bind to create a single sf object. I'm looking for a function similar to data.table::rbindlist , that would stack the individual objects in an efficient manner. Data for reproducible example: my_list <- structure(list(structure(list(idhex = 4L, geometry = structure(list( structure(c(664106.970004623, 6524137.38910266), class = c("XY", "POINT", "sfg"))), class = c("sfc_POINT", "sfc"), precision = 0, bbox = structure(c(xmin = 664106.970004623

R: Transpose the a results table and add column headers

十年热恋 提交于 2020-01-06 16:17:12
问题 Setting the scene: So I have a directory with 50 .csv files in it. All files have unique names e.g. 1.csv 2.csv ... The contents of each may vary in the number of rows but always have 4 columns The column headers are: Date Result 1 Result 2 ID I want them all to be merged together into one dataframe (mydf) and then I'd like to ignore any rows where there is an NA value. So that I can count how many complete instances of an "ID" there were. By calling for example; myfunc("my_files", 1) myfunc(

How can I combine two dataframes with different lengths in R?

ε祈祈猫儿з 提交于 2020-01-04 05:36:12
问题 I have two dataframes, like those: and I want to get something like that: Here are the data frames for reproducibility: df1 <- data.frame(descripcion_cuenta_N2 = c("Consumos", "Costes Personal", "Fungible Equipamiento", "Servicios"), anualidad = rep(2014, 4), valor = c(10, 11, 12, 13)) df2 <- data.frame(descripcion_cuenta_N2 = c("Consumos", "Costes Personal", "Fungible Equipamiento","Prestaciones", "Servicios"), anualidad = rep(2014, 5), valor = c(11, 20, 8, 9)) A dataframe in which missed

Why rbind converts a list of numeric elements to a character matrix?

蹲街弑〆低调 提交于 2020-01-04 03:14:08
问题 Why does rbind convert my list of numeric elements to character? > class(mymatrix.list) [1] "list" > class(mymatrix.list[[1]]) [1] "numeric" > mymatrix.results = do.call(rbind, mymatrix.list) > class(mymatrix.results) [1] "matrix" > class(mymatrix.results[1]) [1] "character" 回答1: Probably because one of the items in your list contains characters? mymatrix.list <- list() for(i in 1:10){ mymatrix.list[[i]] <- rnorm(26) } class(mymatrix.list) # [1] "list" class(mymatrix.list[[1]]) # [1] "numeric

Duplicate the rows based on some criteria in SQL or R

…衆ロ難τιáo~ 提交于 2020-01-01 17:09:19
问题 I use R to generate a toy set data.frame(name = c("Tom", "Shane", "Daniel", "Akira", "Jack", "Zoe"), c1 = c(1,2,3,0,5,0), c2 = c(0, 3, 5, 0,4,0), c3 = c(0, 0,1,0,0,3), c4=c(0,0,0,1,0,0)) which is displayed below: I only care about the columns c1, c2, c3, c4 , and if a specific row has more than one value, which is greater than 0. we need to duplicate rows to make sure that there are only one value, which is greater than 0, and then remove the original row. For instance, the second row has two

Why does rbindlist not respect column names?

a 夏天 提交于 2019-12-30 18:05:08
问题 I just discovered this bug, only to find that some people are calling it a "feature". This makes rbindlist NOT like do.call("rbind",l) as rbind WILL respect column names. Further, there is no mention of this entirely unexpected behavior in the documentation. Is this really intentional? Code example: > library(data.table) > DT1 <- data.table(a=1, b=2) > DT2 <- data.table(b=3, a=4) > DT1 a b 1: 1 2 > DT2 b a 1: 3 4 I would expect that rbind 'ing these would produce columns with a = 1,4 ; b = 2

Print a multiplication table with minimal code

試著忘記壹切 提交于 2019-12-29 08:22:35
问题 In R, what is the fastest way(shortest code) to print multiplication table? The functions seq rep and the bind functions help, but I'm looking for the shortest line(s) of code to do this. rbind("1\'s"=1:12, "2\'s"=seq(2,24,2), "3\'s"=seq(3,36,3), "4\'s"=seq(4,48,4), "5\'s"=seq(5,60,5), "6\'s"=seq(6,72,6)) Prints the 1's through 6's going across (horizontally). Anyone know how to perform this in a more compact way? 回答1: tbl <- outer(1:6, 1:12, "*") rownames(tbl) <- paste(1:6, "'s", sep="") tbl

Print a multiplication table with minimal code

心已入冬 提交于 2019-12-29 08:22:09
问题 In R, what is the fastest way(shortest code) to print multiplication table? The functions seq rep and the bind functions help, but I'm looking for the shortest line(s) of code to do this. rbind("1\'s"=1:12, "2\'s"=seq(2,24,2), "3\'s"=seq(3,36,3), "4\'s"=seq(4,48,4), "5\'s"=seq(5,60,5), "6\'s"=seq(6,72,6)) Prints the 1's through 6's going across (horizontally). Anyone know how to perform this in a more compact way? 回答1: tbl <- outer(1:6, 1:12, "*") rownames(tbl) <- paste(1:6, "'s", sep="") tbl

R: losing column names when adding rows to an empty data frame

余生长醉 提交于 2019-12-28 11:41:22
问题 I am just starting with R and encountered a strange behaviour: when inserting the first row in an empty data frame, the original column names get lost. example: a<-data.frame(one = numeric(0), two = numeric(0)) a #[1] one two #<0 rows> (or 0-length row.names) names(a) #[1] "one" "two" a<-rbind(a, c(5,6)) a # X5 X6 #1 5 6 names(a) #[1] "X5" "X6" As you can see, the column names one and two were replaced by X5 and X6 . Could somebody please tell me why this happens and is there a right way to