英文:
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 <- colnames(df)[grepl("M", df)]
c_names
[1] "c0" "c2" "c3"
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 == "M")),]
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 %>%
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论