[R] adding vector values to corresponding components of a list
David L Carlson
dcarlson at tamu.edu
Tue Dec 15 18:19:33 CET 2015
With each post you seem to be changing what you are trying to accomplish. Your WRONG way seems pretty close if I understand you at all. Just tag the names to the numeric vector instead of the logical vector, eg:
> names(dbv) <- letters[1:8]
> split(dbv,logmat1)
$`FALSE`
a d g h
1.540183 1.172661 1.038135 1.172661
$`TRUE`
b c e f
1.295202 1.038135 1.540183 1.295202
But the other problem is that you may not realize what is happening. Using logmat1 (a matrix) as a vector in split means that it is converted by columns:
> logmat1
[,1] [,2] [,3] [,4]
[1,] FALSE TRUE TRUE FALSE
[2,] TRUE FALSE TRUE FALSE
> as.vector(logmat1)
[1] FALSE TRUE TRUE FALSE TRUE TRUE FALSE FALSE
Earlier it seemed you wanted to combine the by rows:
c(logmat1[1,], logmat[2,])
[1] FALSE TRUE TRUE FALSE TRUE FALSE TRUE FALSE
As you can see, it makes a difference.
-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352
-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of debra ragland via R-help
Sent: Tuesday, December 15, 2015 9:56 AM
To: R-help
Subject: [R] adding vector values to corresponding components of a list
Hello,
I have tried this 2 ways and I keep coming to a dead end as I am not very proficient in R.
I have a logical matrix, where I would like to generate every row-wise pair of logical values for further testing. That is row1, row2; row 1, row3 etc. Ideally, I would like to assign a pre-generated vector of values to each pair such that each combination of rows may be split into 2 groups with each variable appearing only once.
This is similar to the WRONG way I've been trying to do this;
vector = runif(4, 1.0, 2.0)
logmat<-matrix(c(rep(c(F,T,F),3), rep(c(T,F,T),3), rep(c(T,T,F),3), rep(c(F,F,T),3)), ncol=4)
logmat1 = logmat[1:2,]
dbv=rep(vector, 2)
split(dbv,logmat1)
As you can see there's no proper distinction between what should be absolutely assigned to "TRUE" and what should be assigned to "FALSE". --Note my actual data is named.
To try and correct this mistake on a subset of my data I've been trying to use what I'll call "method 1" where
a = c(TRUE, TRUE, TRUE, FALSE, FALSE, TRUE)
names(a)=c(a,b,c,d,e,f)
b = c(TRUE, TRUE, FALSE, FALSE, TRUE, FALSE)
names(b)=c("a","b","c","d","e","f")
Using this function, I am able to separate which variables are exactly "TRUE" or "FALSE
logical_groups = function (a, b) {
r = list()
s = a + b
r$'TRUE' = names(subset(s, s == 2))
r$'FALSE' = names(subset(s, s == 0))
r$'OR' = names(subset(s, s==1))
return(r)
}
> logical_groups(a,b)
$`TRUE`
[1] "a" "b"
$`FALSE`
[1] "d"
$OR
[1] "c" "e" "f"
The return, as you can see is a list.
I have a vector of values that match the names a-f in a and b above, i.e.
vector=runif(6, 1.0, 2.0)
names(vector) = c("a","b","c","d","e","f")
and I would like to add these values to the corresponding (names in the) list so that I may run the wilcox.test correctly using the 'TRUE' and 'FALSE elements. But I am not sure how to achieve this.
I have also tried "method 2"-- starting with;
t=rbind(a, b, vector) to give;
>t
a b c d e f
a 1.000000 1.000000 1.000000 0.00000 0.000000 1.000000
b 1.000000 1.000000 0.000000 0.00000 1.000000 0.000000
vector 1.012097 1.431088 1.832276 1.12801 1.780018 1.682804
where I then try
t2 =t[1,]+t[2,]
t3=rbind(t2, t[3,]) to give;
> t3
a b c d e f
t2 2.000000 2.000000 1.000000 0.00000 1.000000 1.000000
1.012097 1.431088 1.832276 1.12801 1.780018 1.682804
But again, I am not sure how to split the t3 matrix where the first row == 2 or == 0.
I have been searching but I think I'm just confusing myself. I am very late in my pregnancy and my brain just won't function fully.
I hope this makes sense. Any help is appreciated.
______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list