R 问题。使用 2 个 data.frames 进行元素明智乘法、加法和除法,给定行中缺失数据的数量各不相同

发布于 2024-09-01 15:23:50 字数 721 浏览 1 评论 0原文

我有各种具有相同长度列的 data.frames,我试图将多个 2 行按元素组合在一起,然后将其相加。例如,下面是我想用来执行此操作的两个向量。

> a.1[186,]
q01_a q01_b q01_c q01_d q01_e q01_f q01_g q01_h q01_i q01_j q01_k q01_l q01_m
    3     3     3     3     2     2     2     3     1    NA    NA     2     2

问题

> u.1[186,]
q04_avl_a q04_avl_b q04_avl_c q04_avl_d q04_avl_e q04_avl_f q04_avl_g q04_avl_h q04_avl_i q04_avl_j q04_avl_k q04_avl_l q04_avl_m        
        4         2         3         4         3         4         4         4         3         4         3         3         3`

是不同的行有不同数量的 NA。我想做的是跳过与任何缺失值的乘法(上面示例中的第 10 个和第 11 个位置),然后在加法之后除以相乘的元素数量(上面示例中的 11 个)。大多数行都是完整的,只需乘以 13。

谢谢!

I have a various data.frames with columns of the same length where I am trying to multiple 2 rows together element-wise and then sum this up. For example, below are two vectors I would like to perform this operation with.

> a.1[186,]
q01_a q01_b q01_c q01_d q01_e q01_f q01_g q01_h q01_i q01_j q01_k q01_l q01_m
    3     3     3     3     2     2     2     3     1    NA    NA     2     2

and

> u.1[186,]
q04_avl_a q04_avl_b q04_avl_c q04_avl_d q04_avl_e q04_avl_f q04_avl_g q04_avl_h q04_avl_i q04_avl_j q04_avl_k q04_avl_l q04_avl_m        
        4         2         3         4         3         4         4         4         3         4         3         3         3`

The issue is that various rows have varying numbers of NA's. What I would like to do is skip the multiplication with any missing values ( the 10th and 11th position from my above example), and then after the addition divide by the number of elements that were multiplied (11 from the above example). Most rows are complete and would just be multiplied by 13.

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眉黛浅 2024-09-08 15:23:50
x <- c(3,3,3,3,2,2,2,3,1,NA,NA,2,2)
y <- c(4,2,3,4,3,4,4,4,3,4,3,3,3)

sum(x*y,na.rm = T)/sum(!is.na(x*y))
[1] 8

编辑

如果有因子,首先将它们转换为数字

x <- as.factor(c(3,3,3,3,2,2,2,3,1,NA,NA,2,2))
y <- as.factor(c(4,2,3,4,3,4,4,4,3,4,3,3,3))
xy <- as.numeric(as.character(x)) * as.numeric(as.character(y))
sum(xy,na.rm = T)/sum(!is.na(xy))
[1] 8
x <- c(3,3,3,3,2,2,2,3,1,NA,NA,2,2)
y <- c(4,2,3,4,3,4,4,4,3,4,3,3,3)

sum(x*y,na.rm = T)/sum(!is.na(x*y))
[1] 8

EDIT

In case of factors, first convert them to numeric

x <- as.factor(c(3,3,3,3,2,2,2,3,1,NA,NA,2,2))
y <- as.factor(c(4,2,3,4,3,4,4,4,3,4,3,3,3))
xy <- as.numeric(as.character(x)) * as.numeric(as.character(y))
sum(xy,na.rm = T)/sum(!is.na(xy))
[1] 8
勿忘初心 2024-09-08 15:23:50

在这里取得一些进展。我使用 Fseries 包中的 ReplaceNA 将所有缺失值更改为零。

> sum(a.11[186,]*u.11[186,])/min((rowSums(a.11!=0)), rowSums(u.11!=0))

现在我只需要让这个工作循环起来!

Making some progress here. I used substituteNA from the Fseries package to change all the missing values to zeros.

> sum(a.11[186,]*u.11[186,])/min((rowSums(a.11!=0)), rowSums(u.11!=0))

Now I just need to make this work in a loop!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文