标签: cbind

使用空df cbind一个df(cbind.fill?)

我想我正在寻找rbind.fill(在Hadley的plyr包装中)的类比cbind.我看了,但没有cbind.fill.

我想做的是以下内容:

#set these just for this example
one_option <- TRUE
diff_option <- TRUE

return_df <- data.frame()

if (one_option) {
    #do a bunch of calculations, produce a data.frame, for simplicity the following small_df
    small_df <- data.frame(a=1, b=2)
    return_df <- cbind(return_df,small_df)
}

if (diff_option) {
    #do a bunch of calculations, produce a data.frame, for simplicity the following small2_df
    small2_df <- data.frame(l="hi there", m=44)
    return_df <- cbind(return_df,small2_df)
}

return_df
Run Code Online (Sandbox Code Playgroud)

可以理解,这会产生错误:

Error in data.frame(..., check.names …
Run Code Online (Sandbox Code Playgroud)

r dataframe cbind

39
推荐指数
3
解决办法
2万
查看次数

大熊猫相当于R的cbind(垂直连接/堆栈向量)

假设我有两个数据帧:

import pandas
....
....
test1 = pandas.DataFrame([1,2,3,4,5])
....
....
test2 = pandas.DataFrame([4,2,1,3,7])
....
Run Code Online (Sandbox Code Playgroud)

我试过test1.append(test2)但它相当于R的rbind.

如何将两者组合成一个类似于cbindR中函数的数据帧的两列?

concat python-3.x pandas cbind

29
推荐指数
2
解决办法
4万
查看次数

为`data.frame`发送`rbind`和`cbind`

背景

的调度机制R的功能rbind()cbind()是非标准.当其中一个论点是a时,我探索了写作rbind.myclass()cbind.myclass()函数的一些可能性data.frame,但到目前为止我还没有一个令人满意的方法.这篇文章集中于rbind,但同样适用cbind.

问题

让我们创建一个rbind.myclass()函数,只需在调用时回显.

rbind.myclass <- function(...) "hello from rbind.myclass"
Run Code Online (Sandbox Code Playgroud)

我们创建了一个类的对象,myclass以下调用rbind所有正确的调度rbind.myclass()

a <- "abc"
class(a) <- "myclass"
rbind(a, a)
rbind(a, "d")
rbind(a, 1)
rbind(a, list())
rbind(a, matrix())
Run Code Online (Sandbox Code Playgroud)

但是,当其中一个参数(这不一定是第一个)时,rbind()将调用base::rbind.data.frame():

rbind(a, data.frame())
Run Code Online (Sandbox Code Playgroud)

这种行为有点令人惊讶,但它实际上已在文档中 dispatch记录rbind().给出的建议是:

如果要将其他对象与数据帧组合在一起,可能需要先将它们强制转换为数据帧.

实际上,这个建议可能难以实施.转换为数据框可能会删除基本类信息.此外,在发出命令之后,可能不知道该建议的用户可能会遇到错误或意外结果rbind(a, x).

途径

警告用户

第一种可能性是警告用户在数据帧rbind(a, x)时不应该进行呼叫x.相反,包的用户mypackage应该显式调用隐藏函数:

mypackage:::rbind.myclass(a, x)
Run Code Online (Sandbox Code Playgroud)

这可以完成,但用户必须记住在需要时进行显式调用.调用隐藏函数是最后的手段,不应该是常规策略. …

r dispatch rbind cbind

17
推荐指数
2
解决办法
555
查看次数

cbind:有没有办法将缺失值设置为NA?

如果我错过了这么简单的问题的答案,请原谅我.

我想用来cbind()绑定两列.其中一个是长度较短的单个条目.

我可以NA为缺失值提供R 吗?

文档讨论了一个deparse.level论点,但这似乎不是我的解决方案.

此外,如果我可能如此大胆,是否还有一种快速的方法来将较短的列添加到NA's?

r cbind

14
推荐指数
2
解决办法
2万
查看次数

是否可以跳过"+"运算符中的NA值?

我想在R中计算一个等式.我不想使用该函数,sum因为它返回1个值.我想要完整的值向量.

x = 1:10
y = c(21:29,NA)
x+y
 [1] 22 24 26 28 30 32 34 36 38 NA

x = 1:10
y = c(21:30)
x+y
 [1] 22 24 26 28 30 32 34 36 38 40
Run Code Online (Sandbox Code Playgroud)

我不想要:

sum(x,y, na.rm = TRUE)
[1] 280
Run Code Online (Sandbox Code Playgroud)

哪个不返回矢量.

这是一个玩具示例,但我有一个更复杂的等式,使用长度为84647个元素的多个向量.

这是我的另一个例子:

x = 1:10
y = c(21:29,NA)
z = 11:20
a = c(NA,NA,NA,30:36)
5 +2*(x+y-50)/(x+y+z+a) 
 [1]       NA       NA       NA 4.388889 4.473684 4.550000 4.619048 4.681818 4.739130       NA
Run Code Online (Sandbox Code Playgroud)

r sum vector lapply cbind

13
推荐指数
5
解决办法
1682
查看次数

cbind具有不同行数的2个数据帧

我有两个名为h和的列表g.它们每个包含244个数据帧,它们如下所示:

h[[1]]
   year  avg    hr   sal
1  2010  0.300  31   2000
2  2011  0.290  30   4000
3  2012  0.275  14    600
4  2013  0.280  24    800 
5  2014  0.295  18   1000
6  2015  0.330  26   7000
7  2016  0.315  40   9000

g[[1]]
   year  pos  fld     
1  2010  A   0.990
2  2011  B   0.995
3  2013  C   0.970
4  2014  B   0.980
5  2015  D   0.990
Run Code Online (Sandbox Code Playgroud)

我想要cbind这两个数据帧.但是如您所见,它们具有不同的行数.我想组合这些数据帧,以便将具有相同年份的行组合在一行中.我希望空的空间充满NA.我期望的结果如下:

   year  avg    hr   sal   pos   fld …
Run Code Online (Sandbox Code Playgroud)

r cbind

11
推荐指数
2
解决办法
3万
查看次数

使用Rcpp的R fast cbind矩阵

cbind在R中,在重复调用中相对耗时,但它对于各种数据类型也是强大的.我编写的代码比cbind绑定两个矩阵快3倍.但bind_colsdplyr包装仅仅是100X的速度比cbind.遗憾的是它不能将矩阵作为输入.有人可以使下面的代码更快.另外,如何快速绑定稀疏矩阵?这是我使用的代码:

require( Rcpp )

func <- 'NumericMatrix mmult(NumericMatrix a,NumericMatrix b) {
    //the colnumber of first matrix
    int acoln=a.ncol();
    //the colnumber of second matrix
    int bcoln=b.ncol();
    //build a new matrix, the dim is a.nrow() and acoln+bcoln
    NumericMatrix out(a.nrow(),acoln+bcoln) ;
    for (int j = 0; j < acoln + bcoln; j++) {
        if (j < acoln) {
            out(_,j) = a(_,j);
        } else {
            //put the context in the second matrix to the new …
Run Code Online (Sandbox Code Playgroud)

c++ r rcpp cbind

9
推荐指数
1
解决办法
1884
查看次数

拆分列表每n个元素和cbind,然后是rbind切片

我想切片列表中的每个n元素,cbind切片,然后rbind切片.

我可以使用下面的代码执行此操作(n = 10个元素,列表长度为30个元素).我'手动'选择列表中的每10个元素,然后选择cbind这10个元素切片.然后我rbind那些cbinded切片.

但是,我认为可以通过l*plyin plyr或者dplyr至少其中一些来实现这一点.对于初学者,我现在不知道如何选择列表中的每个n元素,并且似乎不知道找到这个答案的适当搜索词.

dl <- list(c(2L, 1L, 3L, 2L, 1L, 1L, 3L), c(1L, 1L, 2L, 1L, 1L, 2L, 
1L), c(1L, 1L, 2L, 2L, 3L, 3L, 3L), c(1L, 1L, 2L, 2L, 3L, 3L, 
3L), c(1L, 1L, 2L, 2L, 3L, 3L, 3L), c(1L, 1L, 2L, 2L, 3L, 3L, 
1L), c(1L, 1L, 2L, 2L, 3L, 3L, 3L), c(1L, 3L, 2L, 1L, …
Run Code Online (Sandbox Code Playgroud)

r list rbind cbind dplyr

7
推荐指数
1
解决办法
466
查看次数

合并具有不同行数和不同列数的数据框

我有两个具有不同列数和行数的数据框。我想将它们组合成一个数据框。

> month.saf
   Name NCDC    Year    Month   Day HrMn    Temp    Q
244 AP  99999   2014    2       1   0      12       1
245 AP  99999   2014    2       1   300    12.2     1
246 AP  99999   2014    2       1   600    14.4     1
247 AP  99999   2014    2       1   900    18.6     1
248 AP  99999   2014    2       1   1200   18       1
249 AP  99999   2014    2       1   1500   13.6     1
250 AP  99999   2014    2       1   1800   11.8     1
251 AP  99999   2014    2       1 …
Run Code Online (Sandbox Code Playgroud)

merge r dataframe cbind

7
推荐指数
1
解决办法
5万
查看次数

将两个data.frames列表绑定到一个新列表

我有两个data.frames列表.两个列表具有相同的长度,并且在其相应的列表元素中包含拟合的data.frames.所以场景看起来像这样

dfa = data.frame(a=1:3, b = letters[1:3])
dfb = data.frame(x=runif(3))
a = replicate(3, dfa, simplify = FALSE)
b = replicate(3, dfb, simplify = FALSE)
Run Code Online (Sandbox Code Playgroud)

一个明显的解决方案是

lapply(seq_along(a), function(i) cbind(a[[i]], b[[i]]))
Run Code Online (Sandbox Code Playgroud)

但我想知道他们是否可能是一个更好的解决方案.

r lapply dataframe cbind

6
推荐指数
1
解决办法
4933
查看次数

标签 统计

cbind ×10

r ×9

dataframe ×3

lapply ×2

rbind ×2

c++ ×1

concat ×1

dispatch ×1

dplyr ×1

list ×1

merge ×1

pandas ×1

python-3.x ×1

rcpp ×1

sum ×1

vector ×1