Displaying 20 results from an estimated 20000 matches similar to: "Write columns from within a list to a matrix?"
2010 Jun 13
4
Are any values in one list contained within a second list
Silly question, but, can I test to see if any value of list a is contained in
list b without doing a loop? A loop is easy enough, but wanted to see if
there was a cleaner way. By way of example:
List 1: a, b, c, d, e, f, g
List 2: z, y, x, w, v, u, b
Return true, since both lists contain b
List 1: a, b, c, d, e, f, g
List 2: z, y, x, w, v, u, t
Return false, since the lists have no mutual
2012 Dec 04
3
How to find matching columns in a matrix of lists?
Dear R users,
I have a matrix composed of lists:
m <- matrix( list(), nrow=1, ncol=3 )
m[[ 1, 1 ]] <- list("A", "B")
m[[ 1, 2 ]] <- list("A", "C")
m[[ 1, 3 ]] <- list("A", "B")
and want to get the sub-matrix where cells contain "B".
But
m[ , "B" %in% m[ 1, ], drop=F ]
as well as
m[ , "B"
2011 Sep 02
3
merge some columns
Dear all,
I would like to know how to merge columns like:
Input file:
V1 V2 V3 V4 V5 V6
1 G A G G G G
2 A A G A A G
Desired output file:
V1 V2 V3
1 G/A G/G G/G
2 A/A G/A A/G
So for every 2 consecutive columns merge their content into one.
Thanks in advance.
[[alternative HTML version deleted]]
2012 Aug 01
4
apply function over same column of all objects in a list
Hello. Please forgive me if this problem has already been posted (and solved)
by someone else ... I can't find it anywhere though it seems so very basic.
Here it is:
I have a list comprised of several matrices, each of which has two columns.
> list
[[1]]
[,1] [,2]
[1,] 1 3
[2,] 2 4
[[2]]
[,1] [,2]
[1,] 5 7
[2,] 6 8
[[3]]
[,1] [,2]
2011 Aug 29
3
Basic question about re-writing for loop as a function
Hello-
Sorry to ask a basic question, but I've spent many hours on this now
and seem to be missing something.
I have a loop that looks like this:
mainmat=data.frame(matrix(data=0, ncol=92, nrow=length(predata$Words_MH)))
for(i in 1:length(predata$Words_MH)){
for(j in 1:92){
mainmat[i,j]=ifelse(j %in%
as.numeric(unlist(strsplit(predata$Words_MH[i], split=","))),
2011 Jul 12
1
applying function to multiple columns of a matrix
Hi,
I want to apply a function to a matrix, taking the columns 3 by 3. I could use a for loop:
for(i in 1:3){ # here I assume my data matrix has 9 columns
j = i*3
set = my.data[,c(j-2,j-1,j)]
my.function(set)
}
which looks cumbersome and possibly slow. I was hoping there is some function in the apply()/lapply() families that could take 3 columns at a time.
I though of turning mydata in a
2007 Jan 19
4
Vectorize rearrangement within each column
Consider a matrix like
> ma = matrix(10:15, nr = 3)
> ma
[,1] [,2]
[1,] 10 13
[2,] 11 14
[3,] 12 15
I want to rearrange each column according to row indexes (1 to 3)
given in another matrix, as in
> idx = matrix(c(1,3,2, 2,3,1), nr = 3)
> idx
[,1] [,2]
[1,] 1 2
[2,] 3 3
[3,] 2 1
The new matrix mb will have for each column the
2006 Sep 03
3
Merge list to list - as list
Dear all,
#Last week, I asked about merge x and y as list.
#Now I have a dataset with list of list like:
x <- list(list(matrix(1:20, 5, 4),matrix(1:20, 5, 4)),
list(matrix(1:20, 5, 4),matrix(1:20, 5, 4)))
y <- list(list(c(1, -1, -1, 1, 1),c(1, 1, -1, -1, -1)),
list(c(1, 1, 1, 1, 1),c(1, 1, -1, 1, -1)))
x
y
#I need merge x and y, I have tried with
list.uni <-
2006 Feb 02
2
Titles in plots generated within tapply
How would one go about putting titles in each of several plots
that are generated from within a call to tapply? For example I'd
like the following two barplots to have titles 'Group 1' and
'Group 2', where '1' and '2' come from the levels of 'group'.
group <- gl(2, 10)
result <- sample(c('A', 'B'), size=length(group),
2011 Aug 26
3
How to vectorize a function to handle two vectors
Dear R-users
I am trying to "vectorize" a function so that it can handle two vectors of
inputs. I want the function to use phi (a function), k[1] and t[1] for the
first price, and so on for the second, third and fourth price. I tried to do
the mapply, but I dont know how to specify to R what input I want to be
vectors (k and t)(see in the bottom what I tried). I have read the help
file,
2006 Jun 26
2
probably need to se sapply but i can't get it
Hi : I think I need to use sapply but I can't figure this out.
Suppose I have two vectors : tempa ( 4, 6,10 ) and tempb
( 11,23 ,39 )
I want a function that returns 4:11,6:23 and 10:39 as vectors.
I tried :
sapply(1:length(tempa) function (z) seq(tempa[z],tempb[z])
but i got 3 really strange vectors back in the sense that the numbers in them did not make no sense to me. obviously,
i
2013 Dec 06
2
Using assign with mapply
I have a data frame whose first colum contains the names of the variables
and whose second colum contains the values to assign to them:
: kkk <- data.frame(vars=c("var1", "var2", "var3"),
vals=c(10, 20, 30), stringsAsFactors=F)
If I do
: assign(kkk$vars[1], kkk$vals[1])
it works
: var1
[1] 10
However, if I try with mapply
2015 Feb 24
2
intercalar elementos de vectores
Gracias, Carlos.
Habia pensado en algo similar usando sapply():
sapply(seq(1, ncol(vtmp), by = 2), function(i) c(rbind(as.character(vtmp[,
i]), as.character(vtmp[, i+1]))))
Dependiendo de la dimension de los datos, quizas mapply() sea mas eficiente
que sapply().
Saludos cordiales,
Jorge.-
2015-02-25 1:01 GMT+11:00 Carlos Ortega <cof en qualityexcellence.es>:
> Hola,
>
> Este
2008 Oct 21
4
Multi matrix row-wise mapply?
Hi group!
Suppose I have 2 matrices A and B of equal dimensions.
I want to apply a function f to all corresponding pairs of rows from A
and B in an efficient manner.
Basically, I want
mapply(f, data.frame(A), data.frame(B))
but for rows.
How do I do it?
Thanks,
Andrey
2009 Jun 23
4
Apply as.factor (or as.numeric etc) to multiple columns
Hi R-helpers,
I have a dataframe with 60columns and I would like to convert several
columns to factor, others to numeric, and yet others to dates. Rather
than having 60 lines like this:
data$Var1<-as.factor(data$Var1)
I wonder if it's possible to write one line of code (per data type,
e.g. factor) that would apply a function (e.g., as.factor) to several
(non-contiguous) columns. So, I
2012 Sep 08
3
Apply a function to columns of a matrix
Dear All,
as a follow up to my previous e-mail (I think I am getting closer...):
I am trying to apply the trapezoidal functions to a matric column by column. I have the following code:
a <-matrix(c(1:100),ncol=10)
b <-matrix(c(2,4,6,8,10,12,14,16,18,20))
apply(a,2,function(b,a) sum(diff(b)*(a[-1]+a[-length(a)]))/2)
for some reason i get an error message:
Error in FUN(newX[[, i],
2011 Feb 02
2
Help me apply mapply
Hello all I would like to ask your help use mapply.
I have a function called findCell that takes two arguments(x,sr)
where x is a vector of size two (e.g x<-c(2,3) and sr is a matrix.
I would like to call many times the findCell function (thus I need mapply) for different x inputs but always for the same sr.
as x is a vector of size two (two cells) I want to pass inside inside the following
2011 Nov 14
1
mapply then export
To use the gauss.quad function: gauss.quad(n,type) which returns two lists
$nodes and $weights whose length will each equal n. I'd like to do this for
n=1 to 40 (type will not change) and have a dataset with 40 rows and 81
columns with all the nodes and weights. The first record would have N1 and
W1 only and N2--N40 and W2--W40 would be missing. The last record would be
full. I've
2008 Oct 07
3
vectorized sub, gsub, grep, etc.
R pattern-matching and replacement functions are
vectorized: they can operate on vectors of targets.
However, they can only use one pattern and replacement.
Here is code to apply a different pattern and replacement
for every target. My question: can it be done better?
sub2 <- function(pattern, replacement, x) {
len <- length(x)
if (length(pattern) == 1)
pattern <-
2011 Jan 08
3
Question on list objects
Hi, I have 2 questions on list object:
1. Suppose I have a matrix like:
dat <- matrix(1:9,3)
Now I want to replicate this entire matrix 3 times and put entire result in a list object. Means, if "res" is the resulting list then I should have:
res[[1]]=dat, res[[2]]=dat, res[[3]]=dat
How can I do that in the easilest manner?
2. Suppose I have 2 list objects:
list1 <- list2