使用`table()`与`for`循环

huangapple go评论109阅读模式
英文:

using table() with for loop

问题

  1. df <- data.frame(class=c('A', 'B'),
  2. var2=c(1, 0),
  3. var3=c(0, 1))
  4. for (i in colnames(df)[2:3]) {
  5. #print(i)
  6. table(paste0('df$', i), df$class)
  7. }

运行结果:

  1. &lt;!-- language: lang-none --&gt;
  2. Error in table(paste0("df$", i), df$class) :
  3. all arguments must have the same length
  4. 也尝试过将
  5. ```R
  6. get(paste0('df$',i))

是否有一种方法可以循环遍历这些列并制表?

英文:
  1. df &lt;- data.frame(class=c(&#39;A&#39;, &#39;B&#39;),
  2. var2=c(1, 0),
  3. var3=c(0, 1))
  4. for (i in colnames(df)[2:3]) {
  5. #print(i)
  6. table(paste0(&#39;df$&#39;, i), df$class)
  7. }

results in

<!-- language: lang-none -->

  1. Error in table(paste0(&quot;df$&quot;, i), df$class) :
  2. all arguments must have the same length

Also tried putting

  1. get(paste0(&#39;df$&#39;,i))

Is there a way to loop through these columns and tabulate?

答案1

得分: 2

  1. 除了将列制表之外,关于你的首选输出没有太多具体信息,但这里有一个可能的解决方案:
  2. ```R
  3. # 这是你的数据框(df):
  4. class<- c('A','B')
  5. var2<- c(1,0)
  6. var3 <- c(0,1)
  7. df<- data.frame(class,var2,var3)
  8. head(df)
  9. class var2 var3
  10. 1 A 1 0
  11. 2 B 0 1
  12. # 使用lapply制表每一列,输出是一个表格列表:
  13. dftable <- lapply(df, table)

输出看起来是这样的:

  1. > dftb
  2. $class
  3. A B
  4. 1 1
  5. $var2
  6. 0 1
  7. 1 1
  8. $var3
  9. 0 1
  10. 1 1

purr包(属于tidyverse)中的map()函数也可以使用:

  1. library(purrr)
  2. df %>%
  3. map(table) # 产生相同的输出
  1. <details>
  2. <summary>英文:</summary>
  3. Not much info on what exactly your preferred output is other than it tabulates the columns, but here&#39;s a potential solution:

This is your df:

class<- c('A','B')
var2<- c(1,0)
var3 <- c(0,1)
df<- data.frame(class,var2,var3)
head(df)

class var2 var3
1 A 1 0
2 B 0 1

Using lapply to tabulate each column. The output is a list of tables:

dftable <- lapply(df, table)

  1. The output looks like this:

> dftb
$class

A B
1 1

$var2

0 1
1 1

$var3

0 1
1 1

  1. The `map()` function from the `purr` package (part of tidyverse) can also be used:

library(purrr)
df|>
map(table) # produces same output

  1. </details>
  2. # 答案2
  3. **得分**: 2
  4. 代码部分不需要翻译,以下是翻译好的部分:
  5. 问题出在您的代码中,因为paste0() 返回一个字符向量,例如 'var2',而不是table()函数的正确参数。您可以使用双方括号 '[[' 来提取列:
  6. ```R
  7. # 创建一个列表以保存循环的结果
  8. tl <- vector(mode = 'list')
  9. # 运行循环,并将每列的结果添加到 'tl' 的相应元素中
  10. for (i in colnames(df)[2:3]) {
  11. tl[[i]] <- table(df[[i]], df$class)
  12. }
  13. # 输出
  14. tl

或者,您可以使用lapply()函数:

  1. lapply(df[, 2:3], function(x) table(x, df$class))

希望这对您有所帮助。

英文:

The issue with your code is that because paste0() returns a character vector e.g, 'var2' and is not a correct argument for table() function. You can use the double bracket '[[' to extract the columns:

  1. # create a list to save the results from loop
  2. tl&lt;-vector(mode = &#39;list&#39;)
  3. # run the loop and add the results for each column in the corresponding element of &#39;tl&#39;
  4. for (i in colnames(df)[2:3]) {
  5. tl[[i]]&lt;-table(df[[i]], df$class)
  6. }

output

  1. tl
  2. $var2
  3. A B
  4. 0 0 1
  5. 1 1 0
  6. $var3
  7. A B
  8. 0 1 0
  9. 1 0 1

alternatively you can use lapply() function:

  1. lapply(df[, 2:3], function(x) table(x, df$class))
  2. var2
  3. x A B
  4. 0 0 1
  5. 1 1 0
  6. $var3
  7. x A B
  8. 0 1 0
  9. 1 0 1

huangapple
  • 本文由 发表于 2023年2月19日 07:07:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75496940.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定