Displaying 20 results from an estimated 20000 matches similar to: "Puzzle or bug with matrix indexing"
2009 Jun 24
1
"by" question
Hello all
I have a big data frame and I regularly want to break it down into subsets,
calculate some new data, and add it back to the data frame.
At the moment my technique seems a bit ugly and embarrassing. Something
like:
result <- by(mydata, mydata$some_factor, function (x) {
# do something to create a vector v with length(v) == nrow(x)
return(v)
})
# now result has a big list, argh...
2018 Jul 30
5
apply with zero-row matrix
Forgive me if this has been asked many times before, but I couldn't find
anything on the mailing lists.
I'd expect apply(m, 1, foo) not to call `foo` if m is a matrix with zero
rows.
In fact:
m <- matrix(NA, 0, 5)
apply(m, 1, function (x) {cat("Called...\n"); print(x)})
## Called...
## [1] FALSE FALSE FALSE FALSE FALSE
Similarly for apply(m, 2,...) if m has no columns.
Is
2009 Jul 28
3
selecting vector elements using matrices and combining the results
I've been scratching my head over this one for too long. I'm hoping
someone out there can solve this riddle.
I have two vectors of characters, v1 and v2, both of length L, and two
matrices of logicals, m1 and m2, both of dimension N*L. The first matrix
of logicals corresponds to the first vector of characters, and the second
to the second.
The matrices are telling me which of the
2018 Jan 04
3
silent recycling in logical indexing
Hmm.
Chuck: I don't see how this example represents
incomplete/incommensurate recycling. Doesn't TRUE replicate from
length-1 to length-3 in this case (mat[c(TRUE,FALSE),2] would be an
example of incomplete recycling)?
William: clever, but maybe too clever unless you really need the
speed? (The clever way is 8 times faster in the following case ...)
x <- rep(1,1e6)
2018 Mar 03
3
install.packages doesn't produce warnings unless qualified with utils::
Hi all,
Assuming this is an R core issue:
tryCatch(install.packages("clipr", repos = "bullshit"), warning = function
(w) cat("got a warning"))
Warning in install.packages :
unable to access index for repository bullshit/src/contrib:
cannot open URL 'bullshit/src/contrib/PACKAGES'
Warning in install.packages :
package ?clipr? is not available (for R
2013 May 17
2
Selecting A List of Columns
Dear R Helpers,
I need help with a slightly unusual situation in which I am trying to
select some columns from a data frame. I know how to use the subset
statement with column names as in:
x=as.data.frame(matrix(c(1,2,3,
1,2,3,
1,2,2,
1,2,2,
1,1,1),ncol=3,byrow=T))
all.cols<-colnames(x)
to.keep<-all.cols[1:2]
Kept<-subset(x,select=to.keep)
Kept
2008 Dec 05
1
array indexing
Hi.
I have been pondering array indexing via matrices.
> a <- array(1:27,rep(3,3))
> index <- matrix(c(1,1,1,1,2,3),2,3,byrow=TRUE)
> a[index]
[1] 1 22
as expected and documented. But what was the thinking
behind the decision to access the array by rows rather
than columns?
The 'index' matrix is ordered as [1,1,1,2,1,3] and so
the extraction is a[index[c(1,3,5)]]
2007 Sep 04
3
variable format
Okay, I want to do something similar to SAS proc format.
I usually do this...
a <- NULL
a$divisionOld <- c(1,2,3,4,5)
divisionTable <- matrix(c(1, "New England",
2, "Middle Atlantic",
3, "East North Central",
4, "West North Central",
5,
2009 Aug 21
1
trouble with Vista & reading files
All,
I am having trouble with a "read.table()" function that is inside of
another function. But if I call the function by itself, it works fine.
Moreover, if I run the script on a Mac OS X (with the default Mac OS X
version of R installed, rev 2.8), it works fine. But it does not work if I
run it on windows vista (also default Windows version of R, rev. 2.8).
Again, both
2012 Jan 10
1
Converting BY to a data.frame
Hello,
I am trying to convert BY to a data frame, consider the following example:
exampleDF<-data.frame(a=c(1,2),b=c(10,20),name=c("first","second"))
exampleBY<-by(exampleDF,with(exampleDF,paste(a,b,sep="_")),
function(x) {
data.frame(
name=as.character(x$name),
a=x$a,
2008 Sep 09
1
puzzle about contrasts
Hi,
I'm trying to redefine the contrasts for a linear model.
With a 2 level factor, x, with levels A and B, a two level
factor outputs A and B - A from an lm fit, say
lm(y ~ x). I would like to set the contrasts so that
the coefficients output are -0.5 (A + B) and B - A,
but I can't get the sign correct for the first coefficient
(Intercept).
Here is a toy example,
set.seed(12161952)
y
2020 Apr 13
2
stringsAsFactors
Further, in addition to the `val <- FALSE` patch a few hours ago by
Martin, the line after should also be changed
- if(!is.logical(val) || is.na(val) || length(val) != 1L)
+ if(!is.logical(val) || length(val) != 1L || is.na(val))
## Consider
Sys.setenv("_R_CHECK_LENGTH_1_LOGIC2_" = "TRUE")
options(stringsAsFactors = c(TRUE, FALSE))
default.stringsAsFactors() # correct
2006 Jun 16
2
inplace assignment
I get tired of writing, e.g.
data.frame[some.condition & another.condition, big.list.of.columns] <-
paste(data.frame[some.condition & another.condition,
big.list.of.columns], "foobar")
I would a function like:
inplace(paste(data.frame[some.condition & another.condition,
big.list.of.columns], "foobar"))
which would take the first argument of the inner
2011 Aug 22
1
Selecting cases from matrices stored in lists
Hi,
I have two lists (c and h - see below) containing matrices with similar
cases but different values. I want to split these matrices into multiple
matrices based on the values in h. So, I did the following:
years<-c(1997:1999)
for (t in 1:length(years))
{
year=as.character(years[t])
h[[year]]<-sapply(colnames(h[[year]]), function(var)
2005 Apr 04
4
acf segfault (PR#7771)
Test case:
z <- ts(matrix(rnorm(200),10,20), start=c(1961,1))
acf(z,lag.max=1)
This segfaults for me. Maybe it shouldn't?
cheers
dave
--please do not edit the information below--
Version:
platform = i386-pc-linux-gnu
arch = i386
os = linux-gnu
system = i386, linux-gnu
status =
major = 2
minor = 0.1
year = 2004
month = 11
day = 15
language = R
Search Path:
.GlobalEnv,
2006 Apr 06
1
Indexing With List Of Vectors (Replacement)
I have the following:
> a <- matrix(1:10, nrow = 2, byrow = TRUE)
> b <- array(as.integer(0), c(7, 5))
> idx <- list()
> length(idx) <- 2
> dim(idx) <- c(1, 2)
> idx[[1]] <- as.integer(1:2)
> idx[[2]] <- as.integer(1:5)
I can do the following, which works if 'b' is a matrix.
> b[idx[[1]], idx[[2]]] <- a
> b
[,1] [,2] [,3] [,4] [,5]
2006 Feb 27
2
heckit with a probit
Hi
I have data for voting behaviour on two (related) binary votes. I want
to examine the second vote, running separate regressions for groups
who voted different ways on the first vote. As the votes are not
independent, I guess that there is an issue with selection bias.
So, I think I would like to fit a heckit style model but with a binary
dependent variable - so, in effect, two successive
2006 May 31
1
interpolating a lot of data at once
I have a big dataset containing a lot of values for 1970, 1980 and
1990. I want to interpolate values for the years in between, and also
if possible to extrapolate to 1968 and 1969. The method doesn't have
to be clever but I am looking for a function that will do all the data
at once. (Doing foreach, or apply, is just too slow!) Is there
something that will take
list(df$val.1970, df$val.1980,
2009 Jul 07
3
how to read point shp file to R?
I am new with R and want do some analysis with a point vector data file. Any
help is appreciate. Sunny
[[alternative HTML version deleted]]
2012 Nov 26
3
Help in splitting the records
Hi
I have set of records seperated by a separator say "$$$" i want to get the
values in a dataframe.
eq
qwer$$12$$qwre
ewrtr$7789$ewwe
I want the output as\
V1 V2 V3
qwer 12 qwre
ewrtr 7789 ewwwe
Please help me
-----
Thanks in Advance
Arun
--
View this message in context: http://r.789695.n4.nabble.com/Help-in-splitting-the-records-tp4650827.html
Sent from