我提供了這個示例數據來獲得我的問題。使用兩列創建新變量滿足一定條件時使用R
aid=c(1,2,3,4,5,6,7,8,9,10)
foson=c(0,1,2,0,6,9,0,0,3,0)
fosof=c(0,0,2,3,0,0,0,5,0,0)
data=data.frame(aid,foson,fosof)
現在,我需要創建一個名爲數據$ HIST符合下列條件的新變量(列):
if foson==0 and fosof==0, then hist = 0;
if foson >=1 and fosof==0, then hist = 1;
if foson==0 and fosof>=1, then hist = 2; and
if foson>=1 and fosof>=1, then hist = 3
我試圖用「ifelse」功能,但不及。
我希望問題很清楚。
感謝所有幫助,
Bazon
嗨Ramnath,工作!我只是想完全理解整行(語法).....尤其是在行末尾的* 2位.Bazon – baz 2010-09-03 01:58:57
它使用邏輯值(TRUE,FALSE)也可以用數字來表示的事實(1,0)。它只是將所有玻色子設置爲1或0,並將所有玻色子設置爲2(1 * 2)或0並添加它們。這實際上是一個很好的解決方案...... C程序員會想到的東西。 – John 2010-09-03 07:53:46