从数据框中删除某些值

时间:2019-06-06 22:48:09

标签: r

我知道已经有一些这样的线程,但是我找不到任何解决方案。

我有一个看起来像这样的数据框:

    Name        Age     Sex     Survived
1   Allison     0.17    female  1
2   Leah        0.33    female  0
3   David       0.8     male    1
4   Daniel      0.83    male    1
5   Alex        0.83    male    1
6   Jay         0.92    male    1
7   Sara        16      female  1
8   Jade        15      female  1
9   Connor      17      male    1
10  Jon         18      male    1
11  Mary        8       female  1

我要删除小于1的年龄。我希望数据看起来像这样:

    Name        Age     Sex     Survived
1   Allison     NA      female  1
2   Leah        NA      female  0
3   David       NA      male    1
4   Daniel      NA      male    1
5   Alex        NA      male    1
6   Jay         NA      male    1
7   Sara        16      female  1
8   Jade        15      female  1
9   Connor      17      male    1
10  Jon         18      male    1
11  Mary        8       female  1

或者仅删除年龄小于1的行。

按照其他解决方案,我尝试了此操作,但没有成功 mydata[mydata$Age<"1"&&mydata$Age>"0"] <- NA

2 个答案:

答案 0 :(得分:2)

以下三种删除行的方法:

mydata[mydata$Age > 1, ]

subset(mydata, Age > 1)

filter(mydata, Age > 1)

这是使它们不适用的方法:

mydata$Age[mydata$Age < 1] <- NA

您的问题是您使用1作为字符(用引号引起来)。字符少于/大于数字的工作方式与数字稍有不同,因此请小心。另外,请确保您的“年龄”列为数字。最好的方法是

mydata$Age <- as.numeric(as.character(mydata$Age))

所以您不会意外弄乱因子变量。

修改 放置错误的标志。立即修复

答案 1 :(得分:0)

struct LoginView : View {
    @State var myText: String = ""

    var body: some View {
        HStack {
            VStack {
                TextField($myText, placeholder: Text("Email"))
                    .textFieldStyle(.roundedBorder)
                    .textContentType(.username)
                TextField($myText, placeholder: Text("Password"))
                    .textFieldStyle(.roundedBorder)
                    .textContentType(.password)
                Button(action: {
                    // Do your login thing here
                }) {
                    Text("Login")
                }
            }
            .relativeWidth(0.7)
        }
        .relativeWidth(1)
    }
}

更新

如果 /// ChatLogMessageCell.swift /** * * I add the target to the UIButton with an image as a background */ messageImage.addTarget(self, action: #selector(fullscreenImage), for: .touchUpInside) /* * Full screen code */ @objc func fullscreenImage() { if let chatlog = parentViewController as? ChatLogController { let imageScroll = UIScrollView() imageScroll.delegate = self imageScroll.minimumZoomScale = 1.0 imageScroll.maximumZoomScale = 5.0 imageScroll.frame = UIScreen.main.bounds let newImageView = UIImageView(image: messageImage.backgroundImage(for: .normal)) newImageView.frame = UIScreen.main.bounds newImageView.backgroundColor = .black newImageView.contentMode = .scaleAspectFit newImageView.isUserInteractionEnabled = true imageScroll.addSubview(newImageView) chatlog.view.addSubview(imageScroll) chatlog.navigationController?.isNavigationBarHidden = true chatlog.tabBarController?.tabBar.isHidden = true let tap = UITapGestureRecognizer(target: self, action: #selector(dismissFullscreenImage)) newImageView.addGestureRecognizer(tap) } } @objc func dismissFullscreenImage(_ sender: UITapGestureRecognizer) { if let chatlog = parentViewController as? ChatLogController { chatlog.navigationController?.isNavigationBarHidden = false chatlog.tabBarController?.tabBar.isHidden = false sender.view?.removeFromSuperview() } } 是因素

,也许可以使用
> mydata[mydata$Age<1, "Age"] <- NA
> mydata
      Name Age    Sex Survived
1  Allison  NA female        1
2     Leah  NA female        0
3    David  NA   male        1
4   Daniel  NA   male        1
5     Alex  NA   male        1
6      Jay  NA   male        1
7     Sara  16 female        1
8     Jade  15 female        1
9   Connor  17   male        1
10     Jon  18   male        1
11    Mary   8 female        1