找到列中最小值,该列的元素是列表,并保留其相应的其他列列表值。

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

How to find the minimum from a column whose elements are list and keep its corresponding other column list valuesg?

问题

df <- data.frame(A = c(1, 2, 3, 4),
conn = c("2, 3, 4", "8, 1, 6", "4, 6, 1, 5", "5, 9, 1, 3"),
ListW = c("100, 100, 100", "60, 100, 100", "100, 50, 100, 100", "100, 10, 100, 100"))

df$ListW <- as.character(df$ListW) # 将ListW列转换为字符类型

listW_values <- strsplit(df$ListW, ", ") # 通过逗号和空格拆分ListW值
min_values <- sapply(listW_values, function(x) min(as.integer(x))) # 找到每个ListW值的最小值

# 创建新的列Nconn和minListW
df$Nconn <- sapply(listW_values, function(x) paste(df$conn[which.min(as.integer(x))], collapse = ", "))
df$minListW <- as.character(min_values)

# 移除原始的conn和ListW列
df <- df[, c("A", "Nconn", "minListW")]
df
英文:

I have data frame

df &lt;- data.frame(A = c(1, 2, 3, 4),
conn = c(&quot;2, 3, 4&quot;, &quot;8, 1, 6&quot;, &quot;4, 6, 1, 5&quot;, &quot;5, 9, 1, 3&quot;),
ListW = c(&quot;100, 100, 100&quot;, &quot;60, 100, 100&quot;, &quot;100, 50, 100, 100&quot;, &quot;100, 10, 100, 100&quot;))

How to find the minimum from ListW and keep its corresponding conn value so that the output would be

df &lt;- data.frame(A = c(1, 2, 3, 4),
Nconn = c(&quot;2, 3, 4&quot;, &quot;8&quot;, &quot;6&quot;, &quot;9&quot;),
minListW = c(&quot;100&quot;, &quot;60&quot;, &quot;50&quot;, &quot;10&quot;))

Your help would be greatly appreciated and helpful for me, others as well. I have tried with the following

df &lt;- data.frame(A = c(1, 2, 3, 4),
                 conn = c(&quot;2, 3, 4&quot;, &quot;8, 1, 6&quot;, &quot;4, 6, 1, 5&quot;, &quot;5, 9, 1, 3&quot;),
                 ListW = c(&quot;100, 100, 100&quot;, &quot;60, 100, 100&quot;, &quot;100, 50, 100, 100&quot;, &quot;100, 10, 100, 100&quot;))

df$ListW &lt;- as.character(df$ListW) # Convert ListW column to character type

listW_values &lt;- strsplit(df$ListW, &quot;, &quot;) # Split the ListW values by comma and space
min_values &lt;- sapply(listW_values, function(x) min(as.integer(x))) # Find the minimum value for each ListW value

答案1

得分: 2

以下是翻译好的代码部分:

library(tidyr)
library(dplyr, warn = FALSE)

df |&gt;
  separate_rows(conn, ListW, sep = &quot;, &quot;, convert = TRUE) |&gt;
  slice_min(ListW, n = 1, by = A) |&gt;
  summarise(across(c(conn, ListW), ~ paste0(unique(.x), collapse = &quot;, &quot;)), .by = A)
#&gt; # A tibble: 4 &#215; 3
#&gt;       A conn    ListW
#&gt;   &lt;dbl&gt; &lt;chr&gt;   &lt;chr&gt;
#&gt; 1     1 2, 3, 4 100  
#&gt; 2     2 8       60   
#&gt; 3     3 6       50   
#&gt; 4     4 9       10

希望这对您有所帮助。如果您有其他问题,请随时提出。

英文:

One option would be to use tidyr::separate_rows to split your columns into rows, then use e.g. dplyr::slice_min to get the minimum(s) per group:

library(tidyr)
library(dplyr, warn = FALSE)

df |&gt;
  separate_rows(conn, ListW, sep = &quot;, &quot;, convert = TRUE) |&gt;
  slice_min(ListW, n = 1, by = A) |&gt;
  summarise(across(c(conn, ListW), ~ paste0(unique(.x), collapse = &quot;, &quot;)), .by = A)
#&gt; # A tibble: 4 &#215; 3
#&gt;       A conn    ListW
#&gt;   &lt;dbl&gt; &lt;chr&gt;   &lt;chr&gt;
#&gt; 1     1 2, 3, 4 100  
#&gt; 2     2 8       60   
#&gt; 3     3 6       50   
#&gt; 4     4 9       10

huangapple
  • 本文由 发表于 2023年6月12日 18:21:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76455683.html
匿名

发表评论

匿名网友

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

确定