# boards

Statistics版 - [R]有没有函数：根据字典翻译一个向量

Dashagen请进请教如何在一数列中找出某个数值的最长连续个数
R question about apply() and outer()R question, calculate mean over some rows

 1 (共1页)
 D*******a发帖数: 207 1比如有字典d： A 1 B 2 C 3 X 5 有向量x： A C X B D 翻译完了是这样的：y 1 3 5 2 D 因为字典里面没有D，所以D保留不翻译。可以optionally删除D。直接用语句来完成比 较繁琐，容易出错，如下： d=c("A","B","C","X","1","2","3","5"); dim(d)=c(4,2) x=c("A","C","X","B","D") sapply(x, FUN=function(item) {if(!(item %in% d)) {item} else {d[which(item = = d[,1]),2]}}) 我自己写过一个y=translation(x,d)，就是把上面那句话包装一下；但是这样一个常用 的东西我想R里面会有现成的函数的。请指教！ s*****n发帖数: 2174 2这个其实很简单, 就用names()即可 d <- c(1,2,3,5) names(d) <- c("A", "B", "C", "X") x <- c("A", "C", "X", "B", "D") x[is.element(x, names(d))] <- d[is.element(x, names(d))] > x [1] "1" "2" "3" "5" "D" D*******a发帖数: 207 3你这个不对，翻译后的顺序错了，不是向量概念了，是set(集合）的概念了。 s*****n发帖数: 2174 4小改动一下即可 > d <- c(1,2,3,5) > names(d) <- c("A", "B", "C", "X") > x <- c("A", "C", "X", "B", "D") > x[is.element(x, names(d))] <- d[x[is.element(x, names(d))]] > x [1] "1" "3" "5" "2" "D" 【在 D*******a 的大作中提到】: 你这个不对，翻译后的顺序错了，不是向量概念了，是set(集合）的概念了。 w*******n发帖数: 469 5so nice to read your R code, I like R more than SAS. 【在 s*****n 的大作中提到】: 小改动一下即可: > d <- c(1,2,3,5): > names(d) <- c("A", "B", "C", "X"): > x <- c("A", "C", "X", "B", "D"): > x[is.element(x, names(d))] <- d[x[is.element(x, names(d))]]: > x: [1] "1" "3" "5" "2" "D"
 1 (共1页)

[R问题]how to make matrix from list (or the other way around)如何把model fitting statistics 读出来（R)

Dashagen请进请教如何在一数列中找出某个数值的最长连续个数
R question about apply() and outer()R question, calculate mean over some rows