从向量中选取一个数字并根据R中的多个条件分配给列

时间:2017-08-30 13:46:27

标签: r

我需要根据多个条件在Products表中添加Plate_Thickness <- c(5.8,25.1,27.1,32.5,55.6,98.1,120.4)列。

1:厚度应该只是这些值中的一个

ThicknessMin

2:厚度应该在表格中已存在的ThicknessMaxProduct ThicknessMin ThicknessMax P0001 0 8 P0002 31.01 70 P0003 8.01 31 P0004 70.01 999 P0005 8.01 31 值之间。

当前表格如下:

ThicknessMin

因此,我们的想法是从矢量中随机选择一个厚度值,但它应该在ThicknessMaxprotocol CustomBackButton { func configureBackButton() func removeBackButton() } extension CustomBackButton where Self: UIViewController { func configureBackButton() { let backButton = UIButton(frame: CGRect(x: 0, y: 0, width: 20, height: 20)) backButton.setImage(#imageLiteral(resourceName: "backIconButton"), for: .normal) backButton.addTarget(self, action: #selector(didTouchBackButton), for: .touchUpInside) self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backButton) self.navigationController?.navigationBar.tintColor = .lightGray } func removeBackButton() { self.navigationItem.hidesBackButton = true } } extension UIViewController { func didTouchBackButton() { self.navigationController?.popViewController(animated: true) } } 之间。请帮助任何指示如何解决这个问题。感谢。

4 个答案:

答案 0 :(得分:2)

矢量化base R解决方案(df是您的data.frame):

set.seed(1) #just for reproducibility
a<-findInterval(df$ThicknessMin,Plate_Thickness,all.inside=TRUE)
b<-findInterval(df$ThicknessMax,Plate_Thickness,all.inside=TRUE)
Plate_Thickness[runif(length(a)) %/% (1/(b-a+1))+a]
#[1]  5.8 32.5 25.1 98.1  5.8

答案 1 :(得分:1)

我们可以将{strong> rowwise 包中的dplyr函数用于sample向量中的Plate_Thickness。在sample的来电中,我们sample仅来自Plate_Thickness betweenThicknessMin的{​​{1}}元素。我把你的表放在ThicknessMax data.frame

dat

数据(再现性)

library(dplyr)
set.seed(123)
dat %>%
    rowwise() %>%
    mutate(thick_sample = sample(Plate_Thickness[between(Plate_Thickness, ThicknessMin, ThicknessMax)],
                          1))

  Product ThicknessMin ThicknessMax thick_sample
   <fctr>        <dbl>        <int>        <dbl>
1   P0001         0.00            8          2.0
2   P0002        31.01           70         55.6
3   P0003         8.01           31         25.1
4   P0004        70.01          999        120.4
5   P0005         8.01           31         27.1

答案 2 :(得分:1)

您的数据

Plate_Thickness <- c(5.8,25.1,27.1,32.5,55.6,98.1,120.4)

df <- structure(list(Product = c("P0001", "P0002", "P0003", "P0004", 
"P0005"), ThicknessMin = c(0, 31.01, 8.01, 70.01, 8.01), ThicknessMax = c(8L, 
70L, 31L, 999L, 31L), Plate_Thickness = c(5.8, 32.5, 27.1, 120.4, 
25.1)), .Names = c("Product", "ThicknessMin", "ThicknessMax", 
"Plate_Thickness"), row.names = c(NA, -5L), class = c("data.table", 
"data.frame"))

溶液

library(dplyr)
acceptable_vals <- lapply(1:nrow(df), function(x) Plate_Thickness[between(Plate_Thickness, df$ThicknessMin[x], df$ThicknessMax[x])])
set.seed(1)
df$Plate_Thickness <- sapply(acceptable_vals, function(x) x[sample(1:length(x), 1)])

输出

   Product ThicknessMin ThicknessMax Plate_Thickness
1:   P0001         0.00            8             5.8
2:   P0002        31.01           70            32.5
3:   P0003         8.01           31            27.1
4:   P0004        70.01          999           120.4
5:   P0005         8.01           31            25.1

答案 3 :(得分:0)

#DATA
df = structure(list(Product = c("P0001", "P0002", "P0003", "P0004", 
"P0005"), ThicknessMin = c(0, 31.01, 8.01, 70.01, 8.01), ThicknessMax = c(8L, 
70L, 31L, 999L, 31L)), .Names = c("Product", "ThicknessMin", 
"ThicknessMax"), class = c("data.table", "data.frame"), row.names = c(NA, 
-5L))

Plate_Thickness = c(5.8,25.1,27.1,32.5,55.6,98.1,120.4)

set.seed(1)
apply(X = df[c("ThicknessMin", "ThicknessMax")],
      MARGIN = 1,        #Run FUN on each row of X
      FUN = function(x)
          #Retain only eligible values for each row and sample 1 value
          sample(x = Plate_Thickness[Plate_Thickness > x[1] & Plate_Thickness < x[2]],
                 size = 1))
#[1]   2.0  32.5  27.1 120.4  25.1