2014-01-19 49 views
1

我有一個數據幀,看起來像這 -應用條件語句在數據幀的所有元素,使用R

> dpd 
     md  mean   sd  fsf  dii n 
2  77.5 0.02827206 0.05761423 0.8382353 29.648895 136 
3  120.0 0.07058824 0.04696682 0.5882353 8.333333 17 
NA  NA   NA   NA  NA   NA NA 
... ... 
NA.8  NA   NA   NA  NA   NA NA 
13 650.0 0.00500000   NA 1.0000000 200.000000 1 
NA.9  NA   NA   NA  NA   NA NA 
.. ... 
NA.12 NA   NA   NA  NA   NA NA 
18 900.0 0.00500000   NA 1.0000000 200.000000 1 

我想打一個if-else語句在這樣一種方式,只有當所有的「 dii'值在數據框中> = 20且'fsf'>> = 0.8時,該功能將打印「GOOD」,否則「您遇到問題!」。於是,我就有點像這個 -

if (dpd$fsf[!is.na(dpd$fsf)] > 0.8 & dpd$dii[!is.na(dpd$dii)] >= 20) 
print("GOOD") else print("You have problem!") 

數據幀清楚地表明,列#3的值不服從這兩個條件,但[R只考慮的第一要素,如下圖所示:

[1] "GOOD" 
Warning message: 
In if (dpd$fsf[!is.na(dpd$fsf)] > 0.8 & dpd$dii[!is.na(dpd$dii)] >= : 
    the condition has length > 1 and only the first element will be used 

如何提高我的條件聲明,以便它顯示「你有問題!」另外,有沒有辦法以我選擇的顏色打印文字「GOOD」?

回答

1

如果你想檢查是否所有邏輯條件評價爲TRUE你應該換行功能all周圍。否則,在ifif中有幾個元素的邏輯向量將僅使用此向量的第一個元素。

x <- 1:3 
y <- 1:3 

x > 2 & y < 3 
[1] FALSE FALSE FALSE 

if (x < 2 & y < 3) print("good") 
[1] "good" 
Warning message: 
In if (x < 2 & y < 3) print("good") : 
    the condition has length > 1 and only the first element will be used 

現在檢查邏輯向量的所有元素都TRUE

all(x > 2 & y < 3) 
[1] FALSE 
if (all(x > 2 & y < 3)) print("good") 
+0

感謝...你知道這個問題的第二部分的回答是:你將如何打印在控制檯顏色文本「GOOD」? – ToNoY

+0

看看這裏:http://stackoverflow.com/questions/17927306/colored-output-in-r-console-esp-colored-errors –

0

你的情況是因爲在fsfdii的NA值的比較複雜一點。您需要在撥打all(...)時使用na.rm=T。使用這種用於DPD:

dpd 
#  id md  mean   sd  fsf  dii n 
# 1  2 77.5 0.02827206 0.05761423 0.8382353 29.648895 136 
# 2  3 120.0 0.07058824 0.04696682 0.5882353 8.333333 17 
# 3 <NA> NA   NA   NA  NA   NA NA 
# 4 NA.8 NA   NA   NA  NA   NA NA 
# 5 13 650.0 0.00500000   NA 1.0000000 200.000000 1 
# 6 NA.9 NA   NA   NA  NA   NA NA 
# 7 NA.12 NA   NA   NA  NA   NA NA 
# 8 18 900.0 0.00500000   NA 1.0000000 200.000000 1 

with(dpd, if(all(fsf>=0.8 & dii>=20)) print("Good") else print("Problem")) 
# [1] "Problem" 

# remove the "bad" item (2nd row) 
dpd.ok <- dpd[-2,] # should print "Good" 
# but it doesn't... 
with(dpd.ok, if(all(fsf>=0.8 & dii>=20)) print("Good") else print("Problem")) 
# Error in if (all(fsf >= 0.8 & dii >= 20)) print("Good") else print("Problem") : 
# missing value where TRUE/FALSE needed 

# setting na.rm=T fixes it 
with(dpd.ok, if(all(fsf>=0.8 & dii>=20,na.rm=T)) print("Good") else print("Problem")) 
# [1] "Good"