试图使用rbind时出现数字错误。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了试图使用rbind时出现数字错误。相关的知识,希望对你有一定的参考价值。
我试图在下面df的底部创建一个额外的行,为每一列创建一个总数。这是用以下方法完成的 rbind(Men_15, colSums(Men_15[1:52,])
. 然而,由于前几行是因子而不是数字,我得到了错误信息:"'x'必须是数字"。我怎样才能让R忽略不相关的列,只对数字列进行求和呢?我还想把行命名为 "USA"。
structure(list(State = structure(1:52, .Label = c("Alabama",
"Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut",
"Delaware", "District of Columbia", "Florida", "Georgia", "Hawaii",
"Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky",
"Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan",
"Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska",
"Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York",
"North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon",
"Pennsylvania", "Puerto Rico", "Rhode Island", "South Carolina",
"South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia",
"Washington", "West Virginia", "Wisconsin", "Wyoming"), class = "factor"),
Men = c(2341093L, 384160L, 3299088L, 1451913L, 19087135L,
2648667L, 1751607L, 448413L, 306674L, 9600009L, 4883331L,
709871L, 810464L, 6316899L, 3235263L, 1534595L, 1439862L,
2164208L, 2261156L, 650081L, 2872643L, 3249650L, 4861973L,
2692166L, 1451723L, 2964003L, 510163L, 929606L, 1407735L,
653484L, 4343027L, 1032414L, 9541801L, 4795408L, 367963L,
5662893L, 1906944L, 1948453L, 6245344L, 1713860L, 510388L,
2322409L, 423477L, 3167756L, 13171316L, 1459229L, 308573L,
4060948L, 3487725L, 913631L, 2851385L, 295561L)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -52L))
答案
你得到的错误来自 colSums
因为你的第一列不是数字。
如果要在列状态中添加 "美国",你可以使用 as.character
. 在这种情况下,你可以简单地使用 sum
获取列的总和 men
.
Men_15$State <- as.character(Men_15$State)
Men_15 <- rbind(Men_15, list("USA", sum(Men_15$Men)))
tail(Men_15)
# State Men
#48 Virginia 4060948
#49 Washington 3487725
#50 West Virginia 913631
#51 Wisconsin 2851385
#52 Wyoming 295561
#53 USA 157448140
要获得 colSums
仅仅是形式 numeric
列,你必须用 is.numeric
喜欢。
colSums(Men_15[sapply(Men_15, is.numeric)])
# Men
#157448140
以上是关于试图使用rbind时出现数字错误。的主要内容,如果未能解决你的问题,请参考以下文章
使用 Kotlin 在片段中引用 RecyclerView 时出现空指针错误
ViewPager - 将数据从片段发送到活动时出现 Android 错误 setCurrentItem(int,boolean)