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

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

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

问题

  1. df <- data.frame(A = c(1, 2, 3, 4),
  2. conn = c("2, 3, 4", "8, 1, 6", "4, 6, 1, 5", "5, 9, 1, 3"),
  3. ListW = c("100, 100, 100", "60, 100, 100", "100, 50, 100, 100", "100, 10, 100, 100"))
  4. df$ListW <- as.character(df$ListW) # 将ListW列转换为字符类型
  5. listW_values <- strsplit(df$ListW, ", ") # 通过逗号和空格拆分ListW值
  6. min_values <- sapply(listW_values, function(x) min(as.integer(x))) # 找到每个ListW值的最小值
  7. # 创建新的列Nconn和minListW
  8. df$Nconn <- sapply(listW_values, function(x) paste(df$conn[which.min(as.integer(x))], collapse = ", "))
  9. df$minListW <- as.character(min_values)
  10. # 移除原始的conn和ListW列
  11. df <- df[, c("A", "Nconn", "minListW")]
  12. df
英文:

I have data frame

  1. df &lt;- data.frame(A = c(1, 2, 3, 4),
  2. conn = c(&quot;2, 3, 4&quot;, &quot;8, 1, 6&quot;, &quot;4, 6, 1, 5&quot;, &quot;5, 9, 1, 3&quot;),
  3. 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

  1. df &lt;- data.frame(A = c(1, 2, 3, 4),
  2. Nconn = c(&quot;2, 3, 4&quot;, &quot;8&quot;, &quot;6&quot;, &quot;9&quot;),
  3. 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

  1. df &lt;- data.frame(A = c(1, 2, 3, 4),
  2. conn = c(&quot;2, 3, 4&quot;, &quot;8, 1, 6&quot;, &quot;4, 6, 1, 5&quot;, &quot;5, 9, 1, 3&quot;),
  3. ListW = c(&quot;100, 100, 100&quot;, &quot;60, 100, 100&quot;, &quot;100, 50, 100, 100&quot;, &quot;100, 10, 100, 100&quot;))
  4. df$ListW &lt;- as.character(df$ListW) # Convert ListW column to character type
  5. listW_values &lt;- strsplit(df$ListW, &quot;, &quot;) # Split the ListW values by comma and space
  6. min_values &lt;- sapply(listW_values, function(x) min(as.integer(x))) # Find the minimum value for each ListW value

答案1

得分: 2

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

  1. library(tidyr)
  2. library(dplyr, warn = FALSE)
  3. df |&gt;
  4. separate_rows(conn, ListW, sep = &quot;, &quot;, convert = TRUE) |&gt;
  5. slice_min(ListW, n = 1, by = A) |&gt;
  6. summarise(across(c(conn, ListW), ~ paste0(unique(.x), collapse = &quot;, &quot;)), .by = A)
  7. #&gt; # A tibble: 4 &#215; 3
  8. #&gt; A conn ListW
  9. #&gt; &lt;dbl&gt; &lt;chr&gt; &lt;chr&gt;
  10. #&gt; 1 1 2, 3, 4 100
  11. #&gt; 2 2 8 60
  12. #&gt; 3 3 6 50
  13. #&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:

  1. library(tidyr)
  2. library(dplyr, warn = FALSE)
  3. df |&gt;
  4. separate_rows(conn, ListW, sep = &quot;, &quot;, convert = TRUE) |&gt;
  5. slice_min(ListW, n = 1, by = A) |&gt;
  6. summarise(across(c(conn, ListW), ~ paste0(unique(.x), collapse = &quot;, &quot;)), .by = A)
  7. #&gt; # A tibble: 4 &#215; 3
  8. #&gt; A conn ListW
  9. #&gt; &lt;dbl&gt; &lt;chr&gt; &lt;chr&gt;
  10. #&gt; 1 1 2, 3, 4 100
  11. #&gt; 2 2 8 60
  12. #&gt; 3 3 6 50
  13. #&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:

确定