尺寸数量不正确r -rowsums

发布于 2025-01-29 21:35:19 字数 1832 浏览 3 评论 0原文

我是编码的新手,所以我基本上是在谷歌搜索所有内容,但我无法弄清楚这一点:

我有一个32行和19列的数据框架。我想在三个特定列中计算每行的总和。

我是这样写的:

d10 - 我的数据框架。

compriance_score - 我想要添加

CROMPIANCE_1-3的新列 - 我想要总和

D10$Compliance_score = rowSums(D10[ ,c("Compliance_1", "Compliance_2", "Compliance_3"), drop = FALSE])

我继续遇到错误的列:“尺寸不正确的数量”。<<<<<<<<< /code>

无法弄清楚我在做什么错,或者此错误消息甚至意味着什么。

有什么想法吗?

**编辑:如果我正确理解了什么是复制的例子(这是我的第一次,我希望我得到了正确的 - 如果不是,请让我知道)

> dput(head(D10))
structure(list(PP = c("003", "014", "047", "013", "053", "048"
), MAAS_1 = c("4.0", "4.0", "3.0", "5.0", "3.0", "4.0"), MAAS_2 = 
c("3.0", 
"1.0", "6.0", "4.0", "3.0", "3.0"), MAAS_3 = c("4.0", "5.0", 
"4.0", "3.0", "4.0", "4.0"), MAAS_4 = c("2.0", "2.0", "6.0", 
"2.0", "3.0", "4.0"), MAAS_5 = c("3.0", "3.0", "4.0", "5.0", 
"5.0", "5.0"), MAAS_6 = c("3.0", "3.0", "4.0", "3.0", "2.0", 
"4.0"), MAAS_7 = c("3.0", "3.0", "4.0", "3.0", "3.0", "5.0"), 
    MAAS_8 = c("2.0", "4.0", "4.0", "4.0", "4.0", "4.0"), MAAS_9 
= c("3.0", 
    "4.0", "3.0", "2.0", "4.0", "5.0"), MAAS_10 = c("3.0", "4.0", 
    "4.0", "2.0", "4.0", "4.0"), MAAS_11 = c("2.0", "5.0", "4.0", 
    "4.0", "1.0", "5.0"), MAAS_12 = c("2.0", "5.0", "6.0", "3.0", 
    "3.0", "6.0"), MAAS_13 = c("3.0", "3.0", "5.0", "3.0", "3.0", 
    "2.0"), MAAS_14 = c("3.0", "4.0", "5.0", "4.0", "4.0", "4.0"
    ), MAAS_15 = c("3.0", "5.0", "6.0", "3.0", "5.0", "5.0"), 
    Compliance_1 = c("0.0", "0.0", "0.0", "0.0", "1.0", "0.0"
    ), Compliance_2 = c("1.0", "0.0", "1.0", "0.0", "1.0", "0.0"
    ), Compliance_3 = c("0.0", "0.0", "0.0", "0.0", "0.0", "0.0"
    )), row.names = c(NA, -6L), class = c("tbl_df", "tbl",         
"data.frame"
))
>

这是有道理的吗?

I'm very new to coding so I'm basically googling everything but I couldn't figure this one out:

I have a data frame of 32 rows, and 19 columns. I want to calculate the sum of each row in three specific columns.

I'm writing it like this:

D10 - my data frame.

Compliance_score - the new column I want to add

Compliance_1-3 - the columns I want to sum

D10$Compliance_score = rowSums(D10[ ,c("Compliance_1", "Compliance_2", "Compliance_3"), drop = FALSE])

I keep getting the error: "incorrect number of dimensions".

Can't figure out what I'm doing wrong, or what this error message even means.

Any thoughts?

**editing: if I understood correctly what is a reproduce example (this is my first time, I hope I got this right- if not please let me know)

> dput(head(D10))
structure(list(PP = c("003", "014", "047", "013", "053", "048"
), MAAS_1 = c("4.0", "4.0", "3.0", "5.0", "3.0", "4.0"), MAAS_2 = 
c("3.0", 
"1.0", "6.0", "4.0", "3.0", "3.0"), MAAS_3 = c("4.0", "5.0", 
"4.0", "3.0", "4.0", "4.0"), MAAS_4 = c("2.0", "2.0", "6.0", 
"2.0", "3.0", "4.0"), MAAS_5 = c("3.0", "3.0", "4.0", "5.0", 
"5.0", "5.0"), MAAS_6 = c("3.0", "3.0", "4.0", "3.0", "2.0", 
"4.0"), MAAS_7 = c("3.0", "3.0", "4.0", "3.0", "3.0", "5.0"), 
    MAAS_8 = c("2.0", "4.0", "4.0", "4.0", "4.0", "4.0"), MAAS_9 
= c("3.0", 
    "4.0", "3.0", "2.0", "4.0", "5.0"), MAAS_10 = c("3.0", "4.0", 
    "4.0", "2.0", "4.0", "4.0"), MAAS_11 = c("2.0", "5.0", "4.0", 
    "4.0", "1.0", "5.0"), MAAS_12 = c("2.0", "5.0", "6.0", "3.0", 
    "3.0", "6.0"), MAAS_13 = c("3.0", "3.0", "5.0", "3.0", "3.0", 
    "2.0"), MAAS_14 = c("3.0", "4.0", "5.0", "4.0", "4.0", "4.0"
    ), MAAS_15 = c("3.0", "5.0", "6.0", "3.0", "5.0", "5.0"), 
    Compliance_1 = c("0.0", "0.0", "0.0", "0.0", "1.0", "0.0"
    ), Compliance_2 = c("1.0", "0.0", "1.0", "0.0", "1.0", "0.0"
    ), Compliance_3 = c("0.0", "0.0", "0.0", "0.0", "0.0", "0.0"
    )), row.names = c(NA, -6L), class = c("tbl_df", "tbl",         
"data.frame"
))
>

Does that make sense?

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

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

发布评论

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

评论(2

风透绣罗衣 2025-02-05 21:35:19

您可以尝试一下:

library(tidyverse)
New_D <- D10 %>%
  mutate(Compliance_score = sum(c(Compliance_1, Compliance_2, Compliance_3), na.rm=TRUE))

但是,一个可重现的示例很棒,可以理解错误。

克莱尔

you can try this :

library(tidyverse)
New_D <- D10 %>%
  mutate(Compliance_score = sum(c(Compliance_1, Compliance_2, Compliance_3), na.rm=TRUE))

But a reproducible example would be great to understand the error.

Claire

信愁 2025-02-05 21:35:19

您的问题是您的数据被存储为字符,因此您需要将其转换为类numeric,以便计算总和,即

library(dplyr)

New_D <- df %>%
  mutate(across(starts_with("Compliance"), as.numeric)) %>% 
  mutate(Compliance_score = Compliance_1 + Compliance_2 + Compliance_3)

或使用@claire建议,当您有Na'values时

New_D <- df %>%
  mutate(across(starts_with("Compliance"), as.numeric)) %>% 
  mutate(Compliance_score = sum(c(Compliance_1, Compliance_2, Compliance_3),
                                na.rm=TRUE))

Your problem is that your data is stored as a character, hence you need transform it to the class numeric in order to calculate the sum, i.e.

library(dplyr)

New_D <- df %>%
  mutate(across(starts_with("Compliance"), as.numeric)) %>% 
  mutate(Compliance_score = Compliance_1 + Compliance_2 + Compliance_3)

Or with @Claire suggestion when you have NA'values

New_D <- df %>%
  mutate(across(starts_with("Compliance"), as.numeric)) %>% 
  mutate(Compliance_score = sum(c(Compliance_1, Compliance_2, Compliance_3),
                                na.rm=TRUE))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文