取字母表中第一个字母(R 中)

发布于 2025-01-17 05:09:50 字数 961 浏览 2 评论 0原文

在下面的数据帧df中,

structure(list(Name = c("Gregory", "Jane", "Joey", "Mark", "Rachel", "Phoebe", "Liza"), code = c("xx11-9090", "1367-88uu", "117y-xxxh", "cf56-gh67", "1888-ddf5", "rf52-628u", "hj69-5kk5"), `CLASS IF5` = c("E", "C", "C", "D", "D", "A", "A"), `CLASS AIS` = c("E", 
"C", "C", "D", "D", "A", "A"), `CLASS IPP` = c("C", "C", "C", 
"E", "E", "B", "A"), `CLASS SJR` = c("D", "C", "C", "D", "D", 
"B", "A")), row.names = c(1682L, 1683L, 1768L, 333L, 443L, 510L, 
897L), class = "data.frame")

字母表示排名。例如:A 为第一个位置,B 为第二个位置,依此类推。字母范围在 A 和 E 之间。我想折叠以 CLASS 开头的列(即数据帧的最后四列),仅保留一列,对于数据帧的每一行,仅对应于排名中最高位置的字母。

期望的结果是:

        Name      code new column 
1682 Gregory xx11-9090         C
1683    Jane 1367-88uu         C
1768    Joey 117y-xxxh         C
333     Mark cf56-gh67         D
443   Rachel 1888-ddf5         D
510   Phoebe rf52-628u         A
897     Liza hj69-5kk5         A

In the following dataframe df,

structure(list(Name = c("Gregory", "Jane", "Joey", "Mark", "Rachel", "Phoebe", "Liza"), code = c("xx11-9090", "1367-88uu", "117y-xxxh", "cf56-gh67", "1888-ddf5", "rf52-628u", "hj69-5kk5"), `CLASS IF5` = c("E", "C", "C", "D", "D", "A", "A"), `CLASS AIS` = c("E", 
"C", "C", "D", "D", "A", "A"), `CLASS IPP` = c("C", "C", "C", 
"E", "E", "B", "A"), `CLASS SJR` = c("D", "C", "C", "D", "D", 
"B", "A")), row.names = c(1682L, 1683L, 1768L, 333L, 443L, 510L, 
897L), class = "data.frame")

the letters denote a ranking. For example: A is the first position, B is the second and so on. The letters range between A and E. I would like to collapse the columns that begin with CLASS (i.e., the last four columns of the dataframe) in only one column keeping, for each row of the dataframe, only the letter that corresponds to the highest position in the ranking.

The desired result is:

        Name      code new column 
1682 Gregory xx11-9090         C
1683    Jane 1367-88uu         C
1768    Joey 117y-xxxh         C
333     Mark cf56-gh67         D
443   Rachel 1888-ddf5         D
510   Phoebe rf52-628u         A
897     Liza hj69-5kk5         A

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心的憧憬 2025-01-24 05:09:51

您可以使用 apply 语句将 min 函数应用于每一行,然后将其输出分配给新列:

df$new_column <- apply(df[, grep("^CLASS", names(df))], 1, min, na.rm = TRUE)

You can use the apply statement to apply the min function to each row and then assign its output to a new column:

df$new_column <- apply(df[, grep("^CLASS", names(df))], 1, min, na.rm = TRUE)
这个俗人 2025-01-24 05:09:51

基础 R 中的一个可能的解决方案:

df$new_coolumn <- apply(df, 1, \(x) sort(x[-(1:2)])[1])
df[,c(1,2,7)]

#>         Name      code new_coolumn
#> 1682 Gregory xx11-9090           C
#> 1683    Jane 1367-88uu           C
#> 1768    Joey 117y-xxxh           C
#> 333     Mark cf56-gh67           D
#> 443   Rachel 1888-ddf5           D
#> 510   Phoebe rf52-628u           A
#> 897     Liza hj69-5kk5           A

使用 dplyr:

library(dplyr)

df %>% 
  rowwise %>% 
  mutate(new_column = c_across(starts_with("CLASS")) %>% sort %>% .[1]) %>% 
  select(Name, code, new_column) %>% ungroup

#> # A tibble: 7 × 3
#>   Name    code      new_column
#>   <chr>   <chr>     <chr>     
#> 1 Gregory xx11-9090 C         
#> 2 Jane    1367-88uu C         
#> 3 Joey    117y-xxxh C         
#> 4 Mark    cf56-gh67 D         
#> 5 Rachel  1888-ddf5 D         
#> 6 Phoebe  rf52-628u A         
#> 7 Liza    hj69-5kk5 A

A possible solution in base R:

df$new_coolumn <- apply(df, 1, \(x) sort(x[-(1:2)])[1])
df[,c(1,2,7)]

#>         Name      code new_coolumn
#> 1682 Gregory xx11-9090           C
#> 1683    Jane 1367-88uu           C
#> 1768    Joey 117y-xxxh           C
#> 333     Mark cf56-gh67           D
#> 443   Rachel 1888-ddf5           D
#> 510   Phoebe rf52-628u           A
#> 897     Liza hj69-5kk5           A

Using dplyr:

library(dplyr)

df %>% 
  rowwise %>% 
  mutate(new_column = c_across(starts_with("CLASS")) %>% sort %>% .[1]) %>% 
  select(Name, code, new_column) %>% ungroup

#> # A tibble: 7 × 3
#>   Name    code      new_column
#>   <chr>   <chr>     <chr>     
#> 1 Gregory xx11-9090 C         
#> 2 Jane    1367-88uu C         
#> 3 Joey    117y-xxxh C         
#> 4 Mark    cf56-gh67 D         
#> 5 Rachel  1888-ddf5 D         
#> 6 Phoebe  rf52-628u A         
#> 7 Liza    hj69-5kk5 A
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文