如何在R

时间:2019-02-02 19:22:23

标签: r string

我分析一些基因ID。这些ID如下:

“ ENSG00000189001.9”“ ENSG00000179152.17”“ ENSG00000131374.13”

1-我要删除“。”以及之后的每个字符,例如:

“ENSG00000189001” “ENSG00000179152” “ENSG00000131374”

2-在第一步之后,我想从字符串中删除“”,如下所示:

ENSG00000189001 ENSG00000179152 ENSG00000131374

2 个答案:

答案 0 :(得分:2)

您可以使用gsub

GID = c("ENSG00000189001.9", "ENSG00000179152.17", "ENSG00000131374.13")
GID2 = gsub("\\..*", "", GID)
cat(GID2, "\n")
ENSG00000189001 ENSG00000179152 ENSG00000131374 

请注意,如果您只是键入GID2,您仍然会看到引号。这就是R指示它们是字符串的方式。使用cat仅显示字符串内容

有关正则表达式的一些细节:
\\.匹配每个字符串中找到的第一个句点。
.*之后将匹配所有内容。
gsub将取代部分匹配(周期和一切之后)用“”,即空字符串。

答案 1 :(得分:1)

下面是一些有趣的,与众不同的方式来获得期望的结果。

import UIKit

class ImageViewController: UIViewController {

    @IBOutlet weak var myCircleView: UIImageView!
    @IBOutlet weak var containerView: UIView!

    var myCircleRotation:CGFloat = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        setupImage()
    }

    private func setupImage() {
        let image = UIImage(named: "your-image")
        myCircleView.image = image
        myCircleView.isUserInteractionEnabled = true

        let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(swiping(sender:)))
        swipeLeft.direction = .left
        containerView.addGestureRecognizer(swipeLeft)
        let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(swiping(sender:)))
        swipeRight.direction = .right
        containerView.addGestureRecognizer(swipeRight)
    }

    @objc func swiping(sender: UISwipeGestureRecognizer) {
        // this function only called the first time!
        if sender.direction == UISwipeGestureRecognizer.Direction.left {
            myCircleRotation -= CGFloat(Double.pi/2)
        } else if sender.direction == UISwipeGestureRecognizer.Direction.right {
            myCircleRotation += CGFloat(Double.pi/2)
        }

        UIView.animate(withDuration: 0.5) {
            self.myCircleView.transform = CGAffineTransform(rotationAngle: self.myCircleRotation)
        }
    }
} 

数据:

scan(text=GID, what="", comment.char=".")
# Read 3 items
# [1] "ENSG00000189001" "ENSG00000179152" "ENSG00000131374"
dirname(chartr(".", "/", GID))
# [1] "ENSG00000189001" "ENSG00000179152" "ENSG00000131374"
read.table(text=GID, sep=".", stringsAsFactors=FALSE)$V1
# [1] "ENSG00000189001" "ENSG00000179152" "ENSG00000131374"
stringi::stri_split_fixed(GID, ".", simplify=TRUE)[,1]
# [1] "ENSG00000189001" "ENSG00000179152" "ENSG00000131374"