Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一列有 13 个字符(2000 行)。我想将它们转换为价值。该值应该是分配给特定字符的所有值的总和。例子
c<-"NAHLA" Now lets say N<-1 A<-2 L<-3 H<-2
总和将是 10。
我尝试使用替换功能,但我无法为多个字母这样做:/
sum(replace( ? )) 和所有被 sapply 包围的列
我会这样:
x <- "NAHLA" > key <- c(N = 1, A = 2, L = 3,H = 2) > sum(key[strsplit(x,"")[[1]]]) [1] 10
从您的描述中有点不清楚您的数据实际上是什么格式,但是这种一般策略应该有效。例如,x有更多元素,我可能会这样:
x
sapply(strsplit(x,""),function(y) sum(key[y]))