2013-06-24 25 views
3

我有一個csv文件,一小部分爲如何將值進行比較,以在SAS一檔或R

Optimal 
value  7.35-7.45 4.5-8 5.6-7.9 0 
ID   V1  V2  V3   V4 
1   7.1  5.5  7.6  218 
10   7.8  4.8  6.3  407 
50   7.12  5.2  5.4  140 

(保密的數據改變BCS)

總體是有125主題和大約50個變量

我需要爲每個變量創建值和範圍之間的距離的絕對值。所以,例如對於ID 1:

V1dist = abs(7.1 - 7.35) = .25 
V2dist = 0 because 4.5 < 5.5 < 8 
V3dist = 0 because 5.6 < 7.6 < 7.9 
V4dist = 218 because the optimal value is 0 

某些值在範圍內 - 它們應該得到0.有些值較低,有些值較高。

所以,我有兩個問題:1)如何在數據讀取2)如何創建新的變量

我有機會獲得SAS和R(和Excel,但是......)

+1

Re(1),現在的數據是什麼形式?你的向量範圍可以很容易地輸入到R中:'value.min = c(7.35,4.5,...)'; 'value.max = c(7.45,8,...)'。至於其餘的數據,如果它在製表符分隔的文本文件中,[?read.table](http://stat.ethz.ch/R-manual/R-devel/library/utils/html/ read.table.html)提供了基本的功能。 – gung

+0

數據位於csv文件中。 –

+0

一切都在csv文件中?如果只有數據矩陣「ID V1 ...」在csv文件中,然後「value」在另一個csv文件中,或手動輸入,我認爲這將是最簡單的。對於csv文件,您可以使用'read.table()'w /'sep =「,」'或使用[?read.csv](http://stat.ethz.ch/R-manual/ R-devel的/庫/ utils的/ HTML/read.table.html)。 – gung

回答

4

這裏有一個解決方案。首先我讀取並格式化數據。我讀矩陣跳過第一行(範圍一),然後我讀一行(這可以使用readLines進行優化)。

dat <- read.table(text='value  7.35-7.45 4.5-8 5.6-7.9 0 
ID   V1  V2  V3   V4 
1   7.1  5.5  7.6  218 
10   7.8  4.8  6.3  407 
50   7.12  5.2  5.4  140',header=TRUE,skip=1) 
mm <- as.matrix(dat[,-1]) 
rownames(mm) <- dat[,1] 
rngs <- read.table(text='value  7.35-7.45 4.5-8 5.6-7.9 0 
ID   V1  V2  V3   V4 
1   7.1  5.5  7.6  218 
10   7.8  4.8  6.3  407 
50   7.12  5.2  5.4  140',nrows=1) 
rngs <- lapply(unclass(rngs[1,-1]), 
     function(x)as.numeric(unlist(strsplit(as.character(x),'-')))) 
names(rngs) <- colnames(mm) 

mm 
    V1 V2 V3 V4 
1 7.10 5.5 7.6 218 
10 7.80 4.8 6.3 407 
50 7.12 5.2 5.4 140 
> rngs 
$V1 
[1] 7.35 7.45 

$V2 
[1] 4.5 8.0 

$V3 
[1] 5.6 7.9 

$V4 
[1] 0 

然後我比較值的範圍。我遍歷每列,並使用嵌套ifelse我計算範圍。請注意,我沒有範圍,我重複相同的值。

sapply(names(rngs),function(x) 
{ 
    vec <- mm[,x] 
    inter <- rngs[[x]] 
    if(length(inter)==1) inter <- rep(inter,2) 
    ## within interval 
    ifelse(mm[,x] < inter[2] & mm[,x] > inter[1], 
     0,ifelse(mm[,x] > inter[2], mm[,x]-inter[2], inter[1]-mm[,x])) 

}) 

    V1 V2 V3 V4 
1 0.25 0 0.0 218 
10 0.35 0 0.0 407 
50 0.23 0 0.2 140 
+0

謝謝。看起來不錯 –

+1

@PeterFlom歡迎您。我懷疑你會得到更好的SAS解決方案:) – agstudy

+0

+1。問題:什麼是「浸染」? – gung

相關問題