gpt4 book ai didi

r - 计算因素的出现并在 R 中的行和列中对它们求和

转载 作者:行者123 更新时间:2023-12-02 02:26:42 26 4
gpt4 key购买 nike

我有这张表:

smoke <- matrix(c("one"," ","three","four "," "," ",
" ","two","three","four"," ","Other",
"one"," ","three","four"," "," ",
" "," "," "," ","none"," ",
" "," "," "," "," "," ",
" "," ","three","four"," ","Other",
" "," "," "," ","none"," "
),ncol=6,byrow=TRUE)
colnames(smoke) <- c("Var_1","Var_2", "Var_3","Var_4","None","Other")
rownames(smoke) <- c(1,2,3,4," ",6,7)

烟:

      Var_1  Var_2   Var_3    Var_4   None   Other  

1 one three four
2 two three four Other
3 one three four
4 None

6 three four Other
7 None

现在我想:

  1. 删除空行(例如第 5 行)。数据集的行太多,我可以轻松检测到空行。
  2. 计算行和列中出现的次数。您可能已经注意到,每列中的字符串(变量)是相同的。即相同的名称,相同的长度。但是,“无”列中的值在列总和中应视为 0。

这是我想要的结果:

        Var_1   Var_2    Var_3     Var_4     None    Other   col_sum

1 one three four 3
2 two three four Other 4
3 one three four 3
4 None 0
5 three four Other 3
6 None 0
rowSum 2 1 4 4 2 2

如果这些是数字数据,我已经学会了如何处理类似的问题。但是现在,我不确定如何解决它。感谢您的帮助!

最佳答案

我们可以在矩阵上使用trimws,在逻辑矩阵上用rowSums计算非空

# // trim out the leading/lagging spaces
smoke <- trimws(smoke)
# // subset the rows by removing rows having only blank
smoke1 <- smoke[!!rowSums(smoke != ""),]
# // get the count of non-blank, non-none elements
rowSums(smoke1 != "" & smoke1 != "none")
#1 2 3 4 6 7
#3 4 3 0 3 0
#// and with columns, use `colSums`
colSums(smoke1 != "")
# Var_1 Var_2 Var_3 Var_4 None Other
# 2 1 4 4 2 2

关于r - 计算因素的出现并在 R 中的行和列中对它们求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65543685/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com