debra ragland
2015-Dec-15 15:56 UTC
[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.
Bert Gunter
2015-Dec-15 16:58 UTC
[R] adding vector values to corresponding components of a list
I as not able to make sense of your post. Maybe others can. However, you have posted here several times, now. You should therefore know that providing a reproducible example (providing data by e.g. dput() ) **and showing exactly what output you want** would improve your chance of a helpful response. 'Nuff said. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Tue, Dec 15, 2015 at 7:56 AM, debra ragland via R-help <r-help at r-project.org> wrote:> 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.