piptoma
piptoma

Reputation: 796

R: Extract one value out of a list and paste it into a data frame

I have a list of data frames (list9).

> str(list9)
List of 2
 $ :'data.frame':   64 obs. of  11 variables:
 ..$ list$Stimulus          : Factor w/ 7 levels "108.wav","42.wav",..: 1 1 1 1 1 1 1 1 2 2 ...
 ..$ list$IndicationStandard: num [1:64] 1 0 1 0 1 0 0 0 0 0 ...
 ..$ list$P42               : num [1:64] 0 0 0 0 0 0 0 0 0 0 ...
 ..$ list$P53               : num [1:64] 0 0 0 0 0 0 0 0 0 0 ...
 ..$ list$P64               : num [1:64] 0.375 0.375 0.375 0.375 0.375 0.375 0.375 0.375 0.375 0.375 ...
 ..$ list$P75               : num [1:64] 0.812 0.812 0.812 0.812 0.812 ...
 ..$ list$P86               : num [1:64] 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 ...
 ..$ list$P97               : num [1:64] 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 ...
 ..$ list$P108              : num [1:64] 0.375 0.375 0.375 0.375 0.375 0.375 0.375 0.375 0.375 0.375 ...
 ..$ list$TGdispInd1        : num [1:64] 0.317 0.317 0.317 0.317 0.317 ...
 ..$ list$TGdispInd2        : num [1:64] 0.756 0.756 0.756 0.756 0.756 ...
$ :'data.frame':    64 obs. of  11 variables:
 ..$ list$Stimulus          : Factor w/ 7 levels "108.wav","42.wav",..: 1 1 1 1 1 1 1 1 2 2 ...
 ..$ list$IndicationStandard: num [1:64] 0 0 1 0 1 0 0 0 0 0 ...
 ..$ list$P42               : num [1:64] 0 0 0 0 0 0 0 0 0 0 ...
 ..$ list$P53               : num [1:64] 0 0 0 0 0 0 0 0 0 0 ...
 ..$ list$P64               : num [1:64] 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 ...
 ..$ list$P75               : num [1:64] 0.812 0.812 0.812 0.812 0.812 ...
 ..$ list$P86               : num [1:64] 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 ...
 ..$ list$P97               : num [1:64] 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 ...
 ..$ list$P108              : num [1:64] 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 ...
 ..$ list$TGdispInd1        : num [1:64] 0.351 0.351 0.351 0.351 0.351 ...
 ..$ list$TGdispInd2        : num [1:64] 0.784 0.784 0.784 0.784 0.784 ...

I created a target data frame (result)

> str(result)
'data.frame':   2 obs. of  3 variables:
 $ TGdispInd1: num  0 0
 $ TGdispInd2: num  0 0
 $ subject   : chr  "TG_75ms_Step11_V1-998-1.txt" "TG_75ms_Step11_V1-999-1.txt"

I would like to paste the first value of list$TGdispInd1 and list$TGdispInd2 of each data frame in the list into the data frame "result" (it could also be the mean of list$TGdispInd1 and list$TGdispInd2, since all 64 values are equal).

This is how the resulting data frame should look like

> result
  TGdispInd1 TGdispInd2                     subject
1       .317       .756 TG_75ms_Step11_v1-998-1.txt
2       .351       .784 TG_75ms_Step11_v1-999-1.txt

Does anybody know how to do this?

Upvotes: 1

Views: 71

Answers (1)

akrun
akrun

Reputation: 887118

Try

result[1:2] <- do.call(rbind,lapply(list9, function(x)
                x[1, c('list$TGdispInd1',  'list$TGdispInd2']))

If you are interested in the mean value

result[1:2] <- do.call(rbind, lapply(list9, function(x) 
               colMeans(x[c('list$TGdispInd1',  'list$TGdispInd2'])))

Upvotes: 1

Related Questions