我試圖在dplyr中使用突變來處理字符串,我沒有得到我想要的輸出(請參見下文)而不是操作行按照行,mutate正在採取第一個元素,並向下填充它。我想知道是否有人能夠幫助我理解我做錯了什麼,以及如何調整此代碼以正常工作。R - 突變字符串處理 - 沒有得到我期望的行爲
short.idfun = function(longid)
{
x = strsplit(longid,"_")
y = x[[1]]
study = substr(y[1],8,nchar(y[1]))
subj = y[length(y)]
subj = substr(subj,regexpr("[^0]",subj),nchar(subj)) #remove leading zeros
shortid= paste(study,subj,sep="-")
return(shortid)
}
data = data.frame(test=c("1234567Andy_003_003003","1234567Beth_004_003004","1234567Char_003_003005"),stringsAsFactors=FALSE)
data= mutate(data,shortid=short.idfun(test))
print(data)
#### Below is my output
# test shortid
#1 1234567Andy_003_003003 Andy-3003
#2 1234567Beth_004_003004 Andy-3003
#3 1234567Char_003_003005 Andy-3003
#### This is the behavior I was hoping for
# test shortid
#1 1234567Andy_003_003003 Andy-3003
#2 1234567Beth_004_003004 Beth-3004
#3 1234567Char_003_003005 Char-3005
謝謝,這是一個不錯的方法! –
@AndyStein很高興幫助! –