我想知道如何在R中正确进行矢量计算。
我有这样的数据集。
保持警察毕业hinshi_#0 54722 6945 83674hinshi_#1 13928 1798 21616hinshi_#2 ……
l 是@Maurits Ever的回答中提供(更正)的向量。 我们可以用 purrr :
l
purrr
as.data.frame(purrr::map2(df,l,function(x,y) x/y))
的 结果 强> (可以格式化以删除科学显示):
stay cops graduation 1 2.719241e+00 2.58948546 2.7763620678 2 6.921089e-01 0.67039523 0.7172340567 3 1.590141e-02 0.01826995 0.0160926405 4 1.789406e+00 1.85831469 1.8310438649 5 3.718943e-01 0.34153617 0.3523126949 6 2.981515e-04 0.00000000 0.0001990842 7 3.282151e-01 0.30947054 0.3232132192 8 9.938382e-05 0.00000000 0.0001659035
如果我理解正确,这里有一个选项
l <- c(20124, 2682, 30138) df / sapply(l, rep, each = nrow(df)) # stay cops graduation #hinshi_#0 2.719241e+00 2.58948546 2.7763620678 #hinshi_#1 6.921089e-01 0.67039523 0.7172340567 #hinshi_#2 1.590141e-02 0.01826995 0.0160926405 #hinshi_#3 1.789406e+00 1.85831469 1.8310438649 #hinshi_#4 3.718943e-01 0.34153617 0.3523126949 #hinshi_#5 2.981515e-04 0.00000000 0.0001990842 #hinshi_#6 3.282151e-01 0.30947054 0.3232132192 #hinshi_#7 9.938382e-05 0.00000000 0.0001659035
df <- read.table(text = "stay cops graduation 'hinshi_#0' 54722 6945 83674 'hinshi_#1' 13928 1798 21616 'hinshi_#2' 320 49 485 'hinshi_#3' 36010 4984 55184 'hinshi_#4' 7484 916 10618 'hinshi_#5' 6 0 6 'hinshi_#6' 6605 830 9741 'hinshi_#7' 2 0 5", header = T)