将不同长度的向量绑定到数据帧 [英] cbind a vector of different length to a dataframe

查看:84
本文介绍了将不同长度的向量绑定到数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两个样本的数据框。只有一个样本回答了有关状态焦虑的问卷。
对于这种情况,我使用以下函数 rowSums计算了躯体状态焦虑的向量:

I have a dataframe consisting of two samples. Only one sample has answered a questionnaire about state anxiety. For this case, I have calculated a vector for somatic state anxiety with the following function "rowSums":

som_lp <- rowSums(sample1[,c(1, 7, 8, 10 )+108], na.rm = TRUE)

现在,我想将其添加到我现有的数据帧数据中,但是由于长度不同(数据帧88,som_lp 59),函数 cbind在这里不起作用。

Now I would like to add this to my existing dataframe "data", but the function "cbind" doesn't work here, because of the different lengths (dataframe 88, som_lp 59).

data <- cbind(data, som_lp) 

有人可以帮助我吗?还有另一种选择来计算 som_lp以避免不同的长度吗?

Can anyone help me and is there another option to calculate "som_lp" to avoid the different lengths?

推荐答案

我们可以使用 rowr

library(rowr)
cbind.fill(data, som_lp, fill = NA)

这篇关于将不同长度的向量绑定到数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆