【问题标题】:How to use multiple variables in R condition如何在 R 条件下使用多个变量
【发布时间】:2021-06-23 05:46:06
【问题描述】:

我有三组变量 例如,以下组包含变量

  1. 同情心、相关性、时间、例子
  2. 工作、信用、科学
  3. 行动、响应、高效。

我希望,如果第一组中的一个变量的值为 1,则它必须计数为 1。如果第二组变量之一的值为 1,则它也必须为 1。最后,如果第三组中的一个变量的值为 1,则它也必须为 1。

我对那个代码感到困惑,

if(Compassion > 0 | relevance > 0 | Time > 0 | 
   Exemplification > 0 & credit > 0 | Science > 0 | 
   Work > 0 & Action > 0 | Response > 0 | efficient> 0)

【问题讨论】:

  • 我不明白你想要什么回报。据我了解,如果任何值为 1,您会触发您的 if 条件吗?你为不同的群体而烦恼吗?您能否提供一个代表某些案例的表格?
  • 我有推特数据,第一组是Internalization,第二组是Explanation,第三组是Action。如果第一组、第二组和第三组中的任何变量出现在必须计数为 1 的推文中。实际上,我想知道每组至少有多少条推文具有一个或多个变量。如果变量仅来自一两个组,也可以。要知道一组推文中有多少条变量?有多少推文有来自两组的变量?有多少条推文包含所有三个组的变量?
  • 你能提供一个这个数据的例子吗,我敢打赌它是一个data.frame,也许打电话head(data)可以帮助我们找到如何测试你的代码?看看这里 ;) stackoverflow.com/questions/5963269/…
  • 我在以下链接中有我的数据。 docs.google.com/spreadsheets/d/…

标签: r


【解决方案1】:

您好,这里是您想要的代码示例。请注意,您可以使用它,对其进行变形以检索数据集的不同方面。

# Reproduction of your dataset type (not a copy, sample is a random function). 
# This is the kind of example it is nice to have in your question
df <- data.frame(Compassion = sample(c(1,0), 5, replace = TRUE),
                 relevance = sample(c(1,0), 5, replace = TRUE),
                 Time = sample(c(1,0), 5, replace = TRUE), 
                 Exemplification = sample(c(1,0), 5, replace = TRUE), 
                 credit = sample(c(1,0), 5, replace = TRUE), 
                 Science = sample(c(1,0), 5, replace = TRUE), 
                 Work = sample(c(1,0), 5, replace = TRUE), 
                 Action = sample(c(1,0), 5, replace = TRUE), 
                 Response = sample(c(1,0), 5, replace = TRUE), 
                 efficient = sample(c(1,0), 5, replace = TRUE))

df

# The groups
g1 <- c("Compassion", "relevance", "Time", "Exemplification")
g2 <- c("credit", "Science", "Work")
g3 <- c("Action", "Response", "efficient")

# TRUE/FALSE on each group. As your data is coded in 0/1, a sum by row is efficient.
boolG1 <- rowSums(df[g1]) >= 1
boolG2 <-rowSums(df[g2]) >= 1
boolG3 <-rowSums(df[g3]) >= 1

# extract the rows where the sum is > to 0
df[boolG1 | boolG2 | boolG3,]
# Printing the number of rows, and changing the conditions
sprintf("number of tweet from 3 groups : %d", nrow(df[boolG1 | boolG2 | boolG3,]))
sprintf("number of tweet from 1st group : %d", nrow(df[boolG1,]))
sprintf("number of tweet from 2nd group : %d", nrow(df[boolG2,]))
sprintf("number of tweet from 3rd group : %d", nrow(df[boolG3,]))

# You can also extract percentage ?
paste0(sprintf("percentage of tweet from 3 groups : %d ", 
        nrow(df[boolG1 | boolG2 | boolG3,])/nrow(df)*100), "%")

您尝试使用 if 条件执行此操作,没关系,但您需要将其放入 for 循环中。 R 在矢量化计算时更有效。这个article有更多信息。

编辑

这是一个用维恩图表示数据集的小代码

library(VennDiagram) # you may need to install this package
venn.diagram(
  x = list(g1 = which(boolG1), 
           g2 = which(boolG2), 
           g3 = which(boolG3)),
  filename = 'venn_diagramm.tiff', # be aware it create a file !
)

【讨论】:

  • 非常感谢,亲爱的。这真的帮助我理解了很多。
  • 不要忘记验证答案,以防其他人有同样的问题,我希望你能用 R 度过美好的时光;)首先考虑矢量化是一件非常困难的事情,但一旦你会掌握它,很多问题都会很快解决!
  • 是的,我会这样做的。但是当我运行维恩图的代码时,它总是给我不同的值......为什么会这样
  • 这是因为我使用sample 函数构建了df。此函数在向量c(0,1) 中采样 5 次(并替换其中的元素)。这就像抛硬币一样,是随机的。在您的数据集上使用其余代码应该不是问题,您只需要使用您的 data.frame 而不是 df 对象。我本可以使用您的数据,但使用较小的 data.frame 示例更容易理解。
  • 明白了,再次感谢。我有另一个国家变量,现在如何根据国家变量区分推文。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-01
  • 2017-03-14
  • 2021-07-14
相关资源
最近更新 更多