R 的新手,想知道是否有人可以帮助我
Newbie to R and was wondering if anyone could help me
将我的数据集作为样本
行数
HKSJS_F1
SJSKA_F4
AJSIWAL_F1
SJSKSUE_F3
AKSICLS_F4
AKAASLE_F1
使用 R 我需要:
按结尾对行进行分组,子组 - 由 F* 确定,例如 F1 或 F2
然后我需要计算每个子组的 man 实例,并将 CSV 中的 return 作为我的输出。
我已经使用打印出我的行名
genenames <- row.names(dataset) print(genenames)
但不确定从这里去哪里。
感谢任何帮助。
是否回答:
> library(dplyr)
> dat
col1
1 HKSJS_F1
2 SJSKA_F4
3 AJSIWAL_F1
4 SJSKSUE_F3
5 AKSICLS_F4
6 AKAASLE_F1
> dat %>% group_by(gsub('(.*)_(F.+)','\2',col1)) %>% summarise(Count = n())
`summarise()` ungrouping output (override with `.groups` argument)
# A tibble: 3 x 2
`gsub("(.*)_(F.+)", "\\2", col1)` Count
<chr> <int>
1 F1 3
2 F3 1
3 F4 2
使用的示例数据:
> dput(dat)
structure(list(col1 = c("HKSJS_F1", "SJSKA_F4", "AJSIWAL_F1",
"SJSKSUE_F3", "AKSICLS_F4", "AKAASLE_F1")), class = "data.frame", row.names = c(NA,
-6L))
>
如果您的名字已经在列中,您可以试试这个:
library(tidyverse)
# datset
df <- data.frame(text = c("HKSJS_F1", "SJSKA_F4", "AJSIWAL_F1",
"SJSKSUE_F3", "AKSICLS_F4", "AKAASLE_F1"))
df2 <- df %>%
count(text_ending = str_extract(text, "F[0-9]$"))
write.csv2(df2, file = "yourpath/csvname.csv", row.names = F)
当您的文本存储在行名中时,您可以试试这个:
library(tidyverse)
# dataset
df <- data.frame(text = rep(1, 6))
row.names(df) <- c("HKSJS_F1", "SJSKA_F4", "AJSIWAL_F1",
"SJSKSUE_F3", "AKSICLS_F4", "AKAASLE_F1")
df2 <- df %>%
add_rownames("rowtext") %>%
count(rowname_ending = str_extract(rowtext, "F[0-9]$"))
write.csv2(df2, file = "yourpath/csvname.csv", row.names = F)
在这两个代码块中,您需要调整 write.csv2
中的路径。
将我的数据集作为样本 行数 HKSJS_F1 SJSKA_F4 AJSIWAL_F1 SJSKSUE_F3 AKSICLS_F4 AKAASLE_F1
使用 R 我需要:
按结尾对行进行分组,子组 - 由 F* 确定,例如 F1 或 F2
然后我需要计算每个子组的 man 实例,并将 CSV 中的 return 作为我的输出。
我已经使用打印出我的行名
genenames <- row.names(dataset) print(genenames)
但不确定从这里去哪里。
感谢任何帮助。
是否回答:
> library(dplyr)
> dat
col1
1 HKSJS_F1
2 SJSKA_F4
3 AJSIWAL_F1
4 SJSKSUE_F3
5 AKSICLS_F4
6 AKAASLE_F1
> dat %>% group_by(gsub('(.*)_(F.+)','\2',col1)) %>% summarise(Count = n())
`summarise()` ungrouping output (override with `.groups` argument)
# A tibble: 3 x 2
`gsub("(.*)_(F.+)", "\\2", col1)` Count
<chr> <int>
1 F1 3
2 F3 1
3 F4 2
使用的示例数据:
> dput(dat)
structure(list(col1 = c("HKSJS_F1", "SJSKA_F4", "AJSIWAL_F1",
"SJSKSUE_F3", "AKSICLS_F4", "AKAASLE_F1")), class = "data.frame", row.names = c(NA,
-6L))
>
如果您的名字已经在列中,您可以试试这个:
library(tidyverse)
# datset
df <- data.frame(text = c("HKSJS_F1", "SJSKA_F4", "AJSIWAL_F1",
"SJSKSUE_F3", "AKSICLS_F4", "AKAASLE_F1"))
df2 <- df %>%
count(text_ending = str_extract(text, "F[0-9]$"))
write.csv2(df2, file = "yourpath/csvname.csv", row.names = F)
当您的文本存储在行名中时,您可以试试这个:
library(tidyverse)
# dataset
df <- data.frame(text = rep(1, 6))
row.names(df) <- c("HKSJS_F1", "SJSKA_F4", "AJSIWAL_F1",
"SJSKSUE_F3", "AKSICLS_F4", "AKAASLE_F1")
df2 <- df %>%
add_rownames("rowtext") %>%
count(rowname_ending = str_extract(rowtext, "F[0-9]$"))
write.csv2(df2, file = "yourpath/csvname.csv", row.names = F)
在这两个代码块中,您需要调整 write.csv2
中的路径。