选择行和列

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

R selecting rows and columns

问题

Sure, here are the translated parts:

第一部分:我想要找到所有包含字符串M的列,并将列名存储在一个新的数据框或列表中:

c_names
c0
c2
c3

第二部分:我想要选择所有包含字符串M的行:

nr c0 c1 c2 c3
1 A B M Z
2 Z U C M
4 M L Y E

谢谢!

英文:

I'm new to R and I have 2 questions. So I have a dataframe that looks like this table:

nr c0 c1 c2 c3
1 A B M Z
2 Z U C M
3 N K N D
4 M L Y E

First I would like to find all columns which contains the string M and store the column names in a new dataframe or a list:

c_names
c0
c2
c3

second I would like to select all rows containing the string M:

nr c0 c1 c2 c3
1 A B M Z
2 Z U C M
4 M L Y E

Thank you!

答案1

得分: 4

  • 对于你的第一个问题:
> data.frame(c_names = names(df)[colSums(df == "M") > 0])
  c_names
1      c0
2      c2
3      c3
  • 对于你的第二个问题:
> subset(df, rowSums(df == "M") > 0)
  nr c0 c1 c2 c3
1  1  A  B  M  Z
2  2  Z  U  C  M
4  4  M  L  Y  E
英文:
  • For your first question
> data.frame(c_names = names(df)[colSums(df == "M") > 0])
  c_names
1      c0
2      c2
3      c3
  • For your second question
> subset(df, rowSums(df == "M") > 0)
  nr c0 c1 c2 c3
1  1  A  B  M  Z
2  2  Z  U  C  M
4  4  M  L  Y  E

答案2

得分: 3

我们可以使用 grepl 在字符串中搜索匹配项,输出是逻辑值。

c_names <- colnames(df)[grepl("M", df)]
c_names
[1] "c0" "c2" "c3"

对于第二个问题,我们可以使用 rowSums 找到等于 "M" 的单元格,并将其转换为逻辑向量以进行索引。

df[as.logical(rowSums(df == "M")),]
  nr c0 c1 c2 c3
1  1  A  B  M  Z
2  2  Z  U  C  M
4  4  M  L  Y  E
英文:

We can use grepl to search for match in strings, the output is logical.

c_names &lt;- colnames(df)[grepl(&quot;M&quot;, df)]
c_names
[1] &quot;c0&quot; &quot;c2&quot; &quot;c3&quot;

For the second question, we can use rowSums to find cells that equals "M", and turn that into a logical vector for indexing.

df[as.logical(rowSums(df == &quot;M&quot;)),]
  nr c0 c1 c2 c3
1  1  A  B  M  Z
2  2  Z  U  C  M
4  4  M  L  Y  E

答案3

得分: 2

以下是使用tidyverse方法的组合方式:

library(dplyr)
library(tidyr)

df %>%
  mutate(across(-nr, ~case_when(. == "M" ~ cur_column()), .names = 'new_{col}')) %>%
  unite(c_names, starts_with('new'), na.rm = TRUE, sep = ' ') %>%
  filter(rowSums(across(everything(), ~ . == "M")) > 0)
nr c0 c1 c2 c3 c_names
1  1  A  B  M  Z      c2
2  2  Z  U  C  M      c3
3  4  M  L  Y  E      c0
英文:

Here is combined tidyverse approach:

library(dplyr)
library(tidyr)

df %&gt;% 
  mutate(across(-nr, ~case_when(. == &quot;M&quot; ~ cur_column()), .names = &#39;new_{col}&#39;)) %&gt;% 
  unite(c_names, starts_with(&#39;new&#39;), na.rm = TRUE, sep = &#39; &#39;) %&gt;% 
  filter(rowSums(across(everything(), ~ . == &quot;M&quot;)) &gt; 0)
nr c0 c1 c2 c3 c_names
1  1  A  B  M  Z      c2
2  2  Z  U  C  M      c3
3  4  M  L  Y  E      c0

huangapple
  • 本文由 发表于 2023年5月26日 15:01:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76338348.html
匿名

发表评论

匿名网友

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

确定