2017-05-05 71 views
0
data = data.frame(
focal = c("John","John","John","John","Albert","Charles","Charles","Jay","Jay","Jay"), 
type = c("Baseline","Baseline","Baseline","Baseline","Experimental","Experimental","Experimental","Baseline","Baseline","Baseline"), 
partner5M = c("Martin","Albert","Chris","Chris","John","Albert","Rich","Martin","Albert","Alfred"), 
duration = sample(c(1:50),10), 
header = TRUE 
) 

我需要如下從我的數據幀中提取數據(它的工作原理):對於R中與字符串變量循環

​​3210

的問題是,我需要做的,對於很多個人,而且我還需要能夠快速改變中的哪些條件,其中

所以我希望做的是一個for循環這將大致如下:

indiv = c("indiv1","indiv2","indiv3") #every different individuals in partner5M 
for(indiv in length(indiv)) { 

Xindiv = database[ which (database$Focal == "JohnDoe" & database$Type == "Baseline" & database$Partner5m == "indiv"),24] 
Xindiv = unlist(Xindiv,use.names = FALSE) 
Xindiv = chron(times = Xindiv) 
} 

我想這樣做的結果是:

Xindiv1 = ... 
Xindiv2 = ... 
Xindiv3 = ... 

等,但我不知道如何做出這樣的循環,所以我會非常感謝該方法的任何建議。

乾杯, 最大

+1

商店它在一個列表('Xindiv [逐張]'),你可以聲明循環 – yeedle

+1

外「快速改變條件」是什麼意思?不同的「焦點」,「類型」和「Partner5m」值?你也需要這些數據作爲輸入。我懷疑你真正想要的是創建一個你的需求的數據框和[合併](http://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-對)他們。但[數據的可重現的例子](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)將幫助人們回答。 –

+0

是的,這就是我的意思,並添加一些更多的條件,如日期。我的原始數據庫非常大,所以我會嘗試提供它的一個較輕的版本,只有關鍵的變量(大多數是字符串btw) – Max

回答

1

希望重現你在找什麼因爲我有多大的可能性非常小的背景信息。

#install.packages("chron") 
library(chron) 

data = data.frame(
    focal = c("John","John","John","John","Albert","Charles","Charles","Jay","Jay","Alfred"), 
    type = c("Baseline","Baseline","Baseline","Baseline","Experimental","Experimental","Experimental","Baseline","Baseline","Baseline"), 
    partner5M = c("Martin","Albert","Chris","John","Chris","Albert","Rich","Martin","Albert","Alfred"), 
    duration = sample(c(1:50),10), 
    header = TRUE, 
    stringsAsFactors = FALSE # added this because you want characters for easy comaprisons 
) 
database <- data # to keep your desired naming 
database # you will notice I edited it 
#  focal   type partner5M duration header 
#1  John  Baseline Martin  37 TRUE 
#2  John  Baseline Albert  41 TRUE 
#3  John  Baseline  Chris  18 TRUE 
#4  John  Baseline  John  50 TRUE 
#5 Albert Experimental  Chris  27 TRUE 
#6 Charles Experimental Albert  4 TRUE 
#7 Charles Experimental  Rich  39 TRUE 
#8  Jay  Baseline Martin  13 TRUE 
#9  Jay  Baseline Albert  28 TRUE 
#10 Alfred  Baseline Alfred  6 TRUE 

indiv = unique(database[,"partner5M"]) # every different individual in partner5M 
indiv 
# [1] "Martin" "Albert" "Chris" "John" "Rich" "Alfred" 

# You seek to make 
indivs <- database[which(database[,"focal"] == database[,"partner5M"] & 
     database[,"type"] == "Baseline"), c("partner5M","duration")] 
# partner5M duration 
#4  John  50 
#10 Alfred  6 

ls() # See what objects are loaded in your environment 
#[1] "data"  "database" "indiv" "indivs" 

for(i in 1:nrow(indivs)){ 
    assign(indivs[i,1], chron(times = unlist(indivs[i,2], use.names = F))) 
} 
ls() 
#[1] "Alfred" "data"  "database" "i"  "indiv" "indivs" "John" 

John 
#Time in days: 
#[1] 30 

我相信按照您的要求製作一個新對象最終會填滿您的環境。更好的方法是使單個對象/列表承載所有你感興趣的值:

xindiv <- sapply(indiv, function(x) { 
    xi = chron(times = database[which(database[,"focal"] == x & 
               database[,"partner5M"] == x & 
               database[,"type"] == "Baseline"),"duration"]) 
    xi 
}) 
xindiv <- xindiv[sapply(xindiv, length) != 0] 
xindiv 
#$John 
#Time in days: 
#[1] 30 
# 
#$Alfred 
#Time in days: 
#[1] 23 
+0

我編輯並提供了數據:) – Max

+0

你想要的輸出仍然讓我猜測。你是否明確希望在你的chron(duration列)環境中創建新對象?或者像列表一樣的單個對象? –

+0

請參閱我的編輯 –