在df行中追加值

时间:2016-05-27 13:10:37

标签: r dataframe append

我在dataframe(df)中有一个列,我要为其追加值(不是常量,而是变量)。一个例子将使它更清楚:

         geneID           Sample.290
1        ENSG00000000001  0.4018499
2        ENSG00000000010  0.2694255
3        ENSG00000000100  1.4441846
4        ENSG00000001000 13.7652753
5        ENSG00000010000  2.1552100
6        ENSG00100008586  0.2358481

我想追加字符“ENSG”和多个“000”,以便每个值的总长度为15(包括ENSG)。例如,输出应为:

func styleComponent() {
       // Change clear.png with your own image name.

        backgroundImage = UIImageView(image: UIImage(named: "clear.png"))
        backgroundImage.contentMode = .ScaleAspectFit
        self.addSubview(backgroundImage)
        backgroundImage.translatesAutoresizingMaskIntoConstraints = false

        let imageConstraints = NSLayoutConstraint(item: backgroundImage, attribute: .Trailing, relatedBy: .Equal, toItem: self, attribute: .Trailing, multiplier: 1, constant: -10)
        self.addConstraint(imageConstraints)

        let topConstraints = NSLayoutConstraint(item: backgroundImage, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1, constant: 10)
        self.addConstraint(topConstraints)

        let bottomConstraints = NSLayoutConstraint(item: backgroundImage, attribute: .Bottom, relatedBy: .Equal, toItem: self, attribute: .Bottom, multiplier: 1, constant: -10)
        self.addConstraint(bottomConstraints)

        let leadingConstraints = NSLayoutConstraint(item: backgroundImage, attribute: .Left, relatedBy: .Equal, toItem: self, attribute: .Left, multiplier: 1, constant: 10)
        self.addConstraint(leadingConstraints)
}

5 个答案:

答案 0 :(得分:5)

使用For i = lnLastRow1 To lnTopRow1 Step -1 For Each c In rng If ws1.Range("Q" & i).Value = c.Value Then ws1.Cells(i, lnCols).Value = "KEEP" Worksheets("Sheet2").Range("H" & c.Row).Value = Worksheets("Sheet1").Cells(c.Row, 3).Value ' MODIFY Exit For End If Next c Next i 中的str_pad

stringr

答案 1 :(得分:3)

使用基本功能:

df$geneID <- sapply(df$geneID,function(x) paste("ENSG",
                    paste(rep(0,(15-nchar(x)-nchar("ENSG"))),collapse = ""),x,sep=""))

“15”变量的总长度;

答案 2 :(得分:2)

for i_block in range(n_templates / 64): mask = 0 for i in range(template_length): # Accumulate difference-indicating bits mask |= tables[i_block][word[i]][i] if mask == 0xFFFFFFFF: # All templates differ, we can stop early break for i in range(64): if mask & (1 << i) == 0: print('Match at template ' + (i_block * 64 + i)) 包中的stri_pad_left函数可以执行您想要的操作:

stringi

答案 3 :(得分:1)

或者你可以(使用基本R函数):

# df
     # geneID Sample.290
# 1         1  0.4018499
# 2        10  0.2694255
# 3       100  1.4441846
# 4      1000 13.7652753
# 5     10000  2.1552100
# 6 100008586  0.2358481

a="ENSG00000000000"
df[,'geneID']=sapply(1:nrow(df), function(i) 
paste0(substring(a, 1, 15-nchar(df[i,'geneID'])), df[i,'geneID']))

# > df
           # geneID Sample.290
# 1 ENSG00000000001  0.4018499
# 2 ENSG00000000010  0.2694255
# 3 ENSG00000000100  1.4441846
# 4 ENSG00000001000 13.7652753
# 5 ENSG00000010000  2.1552100
# 6 ENSG00100008586  0.2358481

答案 4 :(得分:0)

我会选择Sotos示例(这是我在阅读你的帖子时立即想到的),str_pad命令