簡體   English   中英

從向量中拆分數據集

[英]Split dataset from a vector

我想 select 使用向量division_value來自df數據集的子集,並為每個df1df2df3應用最小值。

輸入

df <- data.frame(id = c(1, 3, 4, 5, 7, 8, 9), x = runif(7), y = rnorm(7))
df

  id          x            y
  1 0.15316440  0.300897329
  3 0.17532977 -1.348602492
  4 0.02923305  0.573446127
  5 0.50233682 -0.415615162
  7 0.65804355  0.003661438
  8 0.52747538 -0.097006421
  9 0.12545577  2.043525380

division_value <- c(3, 6, 9)

預計 Output

 > df1
 id          x            y
  1 0.15316440  0.300897329
  3 0.17532977 -1.348602492

apply(df1[, -1], 2, min)

 > df2
  id          x            y
  4 0.02923305  0.573446127
  5 0.50233682 -0.415615162

 apply(df2[, -1], 2, min)

 > df3
 id          x            y
  8 0.52747538 -0.097006421
  9 0.12545577  2.043525380

apply(df3[, -1], 2, min)

您可以嘗試使用split的方法:

split.f <- split(f,sapply(f$id,function(x){sum(x > division_value)})+1)
split.f
$`1`
  id         x          y
1  1 0.6516738 -0.4115108
2  3 0.1255551  0.2522234

$`2`
  id         x          y
3  4 0.2672207 -0.8919211
4  5 0.3861141  0.4356833

$`3`
  id          x          y
5  7 0.01339033 -1.2375384
6  8 0.38238796 -0.2242679
7  9 0.86969085  0.3773956

result <- sapply(split.f,function(x){apply(x[,-1],2,min)})
result
           1          2           3
x  0.1255551  0.2672207  0.01339033
y -0.4115108 -0.8919211 -1.23753842

我知道這與您預期的 output 完全不匹配,但這是您真正想要的嗎?

您還可以設置名稱以匹配division_value

colnames(result) <- division_value
result
           3          6           9
x  0.1255551  0.2672207  0.01339033
y -0.4115108 -0.8919211 -1.23753842

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM