尺寸数量不正确r -rowsums
我是编码的新手,所以我基本上是在谷歌搜索所有内容,但我无法弄清楚这一点:
我有一个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试一下:
但是,一个可重现的示例很棒,可以理解错误。
克莱尔
you can try this :
But a reproducible example would be great to understand the error.
Claire
您的问题是您的数据被存储为字符,因此您需要将其转换为类
numeric
,以便计算总和,即或使用@claire建议,当您有Na'values时
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.Or with @Claire suggestion when you have NA'values