2017-05-24 182 views
0
#e.g. each number in df1 needs to be divided by the correponding value in df2 
    Var<- c("t1","t2","t3","t4","t5","t6","t7","t8","t9","t10","t11") 
    value1 <- c(2,3.1,4.5,5.1,6.5,7.1,8.5,9.11,10.1,11.8,12.3) 
    value2 <- c(2.5,3.1,4.5,5.1,12,7.1,8.5,9.11,10.1,17.8,12.3) 

    df_1 <- data.frame(Var,value1,value2) 

    Var<- c("t1","t2","t3","t4","t5","t6","t7","t8","t9","t10","t11") 
    value1 <- c(2.2,3.3,4.5,5.1,6.5,13,0,0,10.1,1,12.3) 
    value2 <- c(2.7,3.3,4.6,5.1,6.5,13,0,0,15.1,5.1,12.3) 

    df_2 <- data.frame(Var,value1,value2) 

鑑於實際的數據幀非常大,我正在尋找一種有效的方法。如何將數據幀的值除以另一個數據幀的相應值?

感謝

+0

'df_1 [-1]/df_2 [-1]' – akrun

回答

1

您可以用第一個除去第一列和cbind之後數學分。

df_3 <- cbind(df_1[1],round(df_1[-1]/df_2[-1],1)) 
+0

謝謝@TheoB。假設我有多個與df_3類似的數據框,並且想要在ggplot中對這些值進行分解,那麼我應該如何重新格式化它們? – Jack

+0

我想,我需要將最終文件(例如df_3)以某種方式轉換爲單列數據幀。由於我不關心價值的身份,只有分配很重要。 – Jack

相關問題