Displaying 20 results from an estimated 10000 matches similar to: "Number of NA's in every second column"
2007 May 18
4
Simple programming question
Hi R-users,
I have a simple question for R heavy users. If I have a data frame like this
dfr <- data.frame(id=1:16, categ=rep(LETTERS[1:4], 4),
var3=c(8,7,6,6,5,4,5,4,3,4,3,2,3,2,1,1))
dfr <- dfr[order(dfr$categ),]
and I want to score values or points in variable named "var3" following this
kind of logic:
1. the highest value of var3 within category (variable named
2007 Mar 20
6
Select the last two rows by id group
Hi R-users,
Following this post http://tolstoy.newcastle.edu.au/R/help/06/06/28965.html ,
how do I get last two rows (or six or ten) by id group out of the data
frame? Here the example gives just the last row.
Sincere thanks,
Lauri
[[alternative HTML version deleted]]
2005 Jun 08
2
get level combinations from "by" list
Dear useRs,
Given this code I end up with a list of class "by":
a <- sample(1:5,200,replace=TRUE)
b <- sample(c("v1","v2","v3"),200,replace=TRUE)
c <- sample(c(11,22,33),200,replace=TRUE)
data <- runif(200)
grouped <- by(data,list(a,b,c),function(x) {c(min=min(x),max=max(x),
median=round(median(x),digits=2),mean=round(mean(x),digits=2))})
2006 Nov 21
2
using nested ifelse and rowSums to create new variable?
Dear R-help community,
If I have a data.frame df as follows:
> df
x1 x2 x3 x4 x5 x6
1 5 5 1 1 2 1
2 5 5 5 5 1 5
3 1 5 5 5 5 5
4 5 5 1 4 5 5
5 5 1 5 2 4 1
6 5 1 5 4 5 1
7 5 1 5 4 4 5
8 5 1 1 1 1 5
9 1 5 1 1 2 5
10 5 1 5 4 5 5
11 1 5 5 2 1 1
12 5 5 5 4 4 1
13 1 5 1 4 4 1
14 1 1 5 4 5 5
15 1 5 5 4
2008 Jan 11
2
How to add rowSums into list?
Hi R-users,
I have a list
a <- list(one=matrix(rnorm(20), 5, 4), two=matrix(rnorm(20, 3, 0.5),5,4))
How to add rowSums (calculated using lapply) to corresponding matrix
in this list
lapply(a, function(x) rowSums(x))
??
-Lauri
2005 Apr 01
7
a R function for sort a data frame.
Is there a R function for sort a data frame by a variable ?
I know sort a vector, but I don't know sort a data frame by a
column. Can you help me ?
the sort() function don't work with data frame.
2006 Jul 10
2
A possible too old question on significant test of correlation matrix
Dear all,
I'm working on a data.frame named en.data, which has n cases and m columns.
I generate the correlation matrix of en.data by
> cor(en.data)
I find that there is no p-value on each correlation in the correlation
matrix. I searched in the R-help mail list and found some related
posts, but I didn't find direct way to solve the problem. Someone said
to use cor.test() or
2006 Aug 31
3
what's wrong with my simulation programs on logistic regression
Dear friends,
I'm doing a simulation on logistic regression model, but the programs can't
work well,please help me to correct it and give some suggestions.
My programs:
data<-matrix(rnorm(400),ncol=8) #sample size is 50
data<-data.frame(data)
names(data)<-c(paste("x",1:8,sep="")) #8 independent variables,x1-x8;
#logistic regression model is
2007 Nov 16
2
expand.grid overflows?
>cbn<-as.matrix(expand.grid( rep( list(0:1), 50)))
Error in rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) :
invalid 'times' value
In addition: Warning message:
In rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) :
NAs introduced by coercion
But I'm only interested in cbn matrix rows where:
cbn<- cbn[rowSums(cbn)==5,]
Is there a way to evaluate it
2005 Mar 16
8
Summing up matrices in a list
Dear all,
I think that my question is very simple but I failed to solve it.
I have a list which elements are matrices like this:
>mylist
[[1]]
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
[[2]]
[,1] [,2] [,3]
[1,] 7 9 11
[2,] 8 10 12
I'd like to create a matrix M<-mylist[[1]]+mylist[[2]]
[,1] [,2] [,3]
[1,] 8 12 16
[2,] 10 14 18
2008 May 08
2
speeding up a special product of three arrays
I am struggling with R code optimization, a recurrent topic on this list.
I have three arrays, say A, B and C, all having the same number of columns.
I need to compute an array D whose generic element is
D[i, j, k] <- sum_n A[i, n]*B[j, n]*C[k, n]
Cycling over the three indices and subsetting the columns won't do. Is there
any way to implement this efficiently in R or should I resign to
2005 Feb 09
4
subset
Dear all,
I am trying to extract rows from a data.frame based on the
rowSums != 0. I want to preserve rownames in the first column in the subset.
Does anyone know how to extract all species that don't have rowSums equal
to zero? Here it is:
# dataset
x <- data.frame(
species=c("sp.1","sp.2","sp.3","sp.4"),
site1=c(2,3,0,0),
site2=c(0,0,0,0),
2007 Jan 05
1
Efficient multinom probs
Dear R-helpers,
I need to compute probabilties of multinomial observations, eg by doing the
following:
y=sample(1:3,15,1)
prob=matrix(runif(45),15)
prob=prob/rowSums(prob)
diag(prob[,y])
However, my question is whether this is the most efficient way to do this.
In the call prob[,y] a whole matrix is computed which seems a bit of a
waste.
Is there maybe a vectorized version of dmultinom which
2009 Sep 08
7
Data separated by spaces, getting data into R using field lengths
I have a text file similar to this (separated by spaces):
x <- "DF12 This is an example 1 This
DF12 This is an 1232 This is
DF14 This is 12334 This is an
DF15 This 23 This is an example
"
and I know the field lengths of each variable (there is 5 variables in
this data set), which are:
varlength <- c(2, 2, 18, 5, 18)
How can I import this kind of data into R, using the varlength
2006 Oct 16
2
Generate a random bistochastic matrix
Please, I would like to generate a random bistochastic matrix, that is a squared matrix of non-negative numbers with each row and each column sum to 1, for example :
.2 .3 .5
.6 .3 .1
.2 .4 .4
I don't know of to code this. Do you have any idea ?
Thanks
Florent Bresson
___________________________________________________________________________
Demandez ?
2006 Nov 14
2
Matrix-vector multiplication without loops
Hi,
I am trying to do the following computation:
p <- rep(0, n)
coef <- runif(K+1)
U <- matrix(runif(n*(2*K+1)), n, 2*K+1)
for (i in 0:K){
for (j in 0:K){
p <- p + coef[i+1]* coef[j+1] * U[,i+j+1]
} }
I would appreciate any suggestions on how to perform this computation
efficiently without the "for" loops?
Thank
2005 May 06
4
how to get such a subset of a matrix?
Hi everybody,
Maybe this question is quite simple but i just don't know how to make it.
I have a matrix a somewhat like this one but bigger:
> a
f g h i j k
a NA NA 11 16 21 26
b NA NA 12 17 22 27
c NA 8 13 18 23 28
d NA 9 14 19 24 29
e NA 10 15 20 25 30
And i want to get the rows which at most have 2 Na.
Thanks in advance.
Shi Jiantao
[[alternative HTML version deleted]]
2008 Feb 21
4
How to get names of a list into df:s?
R users,
I have a simple lapply question.
g <- list(a=1:3, b=4:6, c=7:9)
g <- lapply(g, function(x) as.data.frame(x))
lapply(g, function(x) cbind(x, var1 = rep(names(g), each=nrow(x))[1:nrow(x)]))
I get
$a
x var1
1 1 a
2 2 a
3 3 a
$b
x var1
1 4 a
2 5 a
3 6 a
$c
x var1
1 7 a
2 8 a
3 9 a
And I would like to have
$a
x var1
1 1 a
2 2 a
3 3 a
2008 Aug 20
2
Random sequence of days?
Dear list,
I tried to find a solution for this problem from the archives but
couldn't find any. I would like sample sequence of ten days from
vector d
d <- seq(as.Date("2007-02-12"), as.Date("2008-08-18"), by="days")
so that the days follow each other (sample(d, 10) is not the
appropriate solution). Any ideas?
Best regards,
Lauri
2006 Feb 14
3
Inf values in a matrix
Hello,
I have some Inf values in a matrix, but I don't want to replace them with
some value but rather remove the rows that contain the Inf values. Also I
would like to record the rows which were removed. Is there an easy way to
do this instead of writing loops over the matrix? Thanks.
Ita Cirovic Donev