rbind

Duplicate the rows based on some criteria in SQL or R

大憨熊 提交于 2019-12-04 19:20:33
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 values are greater than 0 (c1: 2, c2: 3), then we have to duplicate that row to two, which looks like

How to append group row into dataframe

帅比萌擦擦* 提交于 2019-12-04 10:13:57
问题 I have this df1: A B C 1 2 3 5 7 9 where A B C are columns names. I have another df2 with one column: A 1 2 3 4 I would like to append df2 for each column of df1, creating this final dataframe: A B C 1 2 3 5 7 9 1 1 1 2 2 2 3 3 3 4 4 4 is it possible to do it? 回答1: data.frame(sapply(df1, c, unlist(df2)), row.names = NULL) # A B C #1 1 2 3 #2 5 7 9 #3 1 1 1 #4 2 2 2 #5 3 3 3 #6 4 4 4 DATA df1 = structure(list(A = c(1L, 5L), B = c(2L, 7L), C = c(3L, 9L)), .Names = c("A", "B", "C"), class =

Losing names of dimnames of a table after cbind or rbind

对着背影说爱祢 提交于 2019-12-04 09:28:44
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 2 3 4 1 1 0 0 4 2 0 0 1 5 3 0 1 0 6 > names(dimnames(y)) <- names(dimnames(x)) > y cols rows 1 2 3 4 1

add missed value based on the value of the column in r

ぃ、小莉子 提交于 2019-12-04 04:48:08
问题 This is my sample dataset: vector1 <- data.frame( "name" = "a", "age" = 10, "fruit" = c("orange", "cherry", "apple"), "count" = c(1, 1, 1), "tag" = c(1, 1, 2) ) vector2 <- data.frame( "name" = "b", "age" = 33, "fruit" = c("apple", "mango"), "count" = c(1, 1), "tag" = c(2, 2) ) vector3 <- data.frame( "name" = "c", "age" = 58, "fruit" = c("cherry", "apple"), "count" = c(1, 1), "tag" = c(1, 1) ) list <- list(vector1, vector2, vector3) print(list) This is my test: default <- c("cherry", "orange",

Combine Vectors inside of a List to Create a Dataframe R [duplicate]

流过昼夜 提交于 2019-12-04 02:35:28
问题 This question already has answers here : Convert a list to a data frame (19 answers) Closed 5 years ago . I have a list of evenly sized characters vectors, and I wish to efficiently combine them into one dataframe, with the vectors in the list to become the rows of the new dataframe. In the following, ls is my list and df is my pre-allocated dataframe. ls <- list(c("r1c1", "r1c2", "r1c3"), c("r2c1", "r2c2", "r2c3")) df <- data.frame(col1 = character(), col2 = character(), col3 = character(),

rbind two data.frame preserving row order and row names

只谈情不闲聊 提交于 2019-12-03 17:24:59
问题 I have a list of data.frame objects which i would like to row append to one another, ie merge(..., all=T) . However, merge seems to remove the row names which I need to be kept intact. Any ideas? Example: x = data.frame(a=1:2, b=2:3, c=3:4, d=4:5, row.names=c("row_1", "another_row1")) y = data.frame(a=c(10,20), b=c(20,30), c=c(30,40), row.names=c("row_2", "another_row2")) > merge(x, y, all=T, sort=F) a b c d 1 1 2 3 4 2 2 3 4 5 3 10 20 30 NA 4 20 30 40 NA 回答1: Since you know you are not

R rbind error row.names duplicates not allowed

为君一笑 提交于 2019-12-03 12:37:50
There are other issues here addressing the same question, but I don't realize how to solve my problem based on it. So, I have 5 data frames that I want to merge rows in one unique data frame using rbind, but it returns the error: "Error in row.names<-.data.frame ( *tmp* , value = value) : 'row.names' duplicated not allowed In addition: Warning message: non-unique values when setting 'row.names': ‘1’, ‘10’, ‘100’, ‘1000’, ‘10000’, ‘100000’, ‘1000000’, ‘1000001 [....]" The data frames have the same columns but different number of rows. I thought the rbind command took the first column as row

Memory efficient alternative to rbind - in-place rbind?

試著忘記壹切 提交于 2019-12-03 09:08:53
问题 I need to rbind two large data frames. Right now I use df <- rbind(df, df.extension) but I (almost) instantly run out of memory. I guess its because df is held in the memory twice. I might see even bigger data frames in the future, so I need some kind of in-place rbind. So my question is: Is there a way to avoid data duplication in memory when using rbind? I found this question, which uses SqlLite, but I really want to avoid using the hard drive as a cache. 回答1: data.table is your friend! C.f

R and rbind making entries without the same length be zero

吃可爱长大的小学妹 提交于 2019-12-03 08:38:13
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 there a better way to build vectors and matrices? All help is greatly appreciated! Ricardo Saporta use

How to append group row into dataframe

只谈情不闲聊 提交于 2019-12-03 06:08:41
I have this df1: A B C 1 2 3 5 7 9 where A B C are columns names. I have another df2 with one column: A 1 2 3 4 I would like to append df2 for each column of df1, creating this final dataframe: A B C 1 2 3 5 7 9 1 1 1 2 2 2 3 3 3 4 4 4 is it possible to do it? data.frame(sapply(df1, c, unlist(df2)), row.names = NULL) # A B C #1 1 2 3 #2 5 7 9 #3 1 1 1 #4 2 2 2 #5 3 3 3 #6 4 4 4 DATA df1 = structure(list(A = c(1L, 5L), B = c(2L, 7L), C = c(3L, 9L)), .Names = c("A", "B", "C"), class = "data.frame", row.names = c(NA, -2L)) df2 = structure(list(A = 1:4), .Names = "A", class = "data.frame", row