Displaying 20 results from an estimated 8000 matches similar to: "Re place only first NA in column"
2009 Nov 10
2
All possible combinations of functions within a function
Dear All,
I wrote a function for cluster analysis to compute cophenetic correlations
between dissimilarity matrices (using the VEGAN library) and cluster
analyses of every possible clustering algorithm (SEE ATTACHED)
http://old.nabble.com/file/p26288610/cor.coef.R cor.coef.R . As it is now,
it is extremely long, and for the future I was hoping to find a more
efficient way of doing this sort of
2004 Mar 11
3
making operators act on rows of a data frame
Dear R helpers,
I wish to use the "sum" operator for each row of a data frame.
However, it appears that the operator acts on the entire data
frame, over all columns. What is the best way to obtain row-
wise operation?
The following code shows my attempts so far, and their problems:-
test1=array(rbinom(120,1,0.5),c(20,3))
test1[,3]=NA
sum(test1[,1:2])
test1[,3][sum(test1[,1:2])>=2]=1
2009 Nov 03
2
Create Artificial Binary Matrix based on probability
Dear All,
I am trying to create an artificial binary matrix such that each cell has a
probability of 0.048 of having a 1. So far the closest I've come is us by
using a random poisson distribution with a mean of 0.048, but I can't figure
out how to limit the max value to 1. Otherwise that would work fine it
seems. Any suggestions?
The main code I've got to create said matrix so far
2007 Aug 09
1
How to apply functions over rows of multiple matrices
Dear ExpRts,
I would like to perform a function with two arguments
over the rows of two matrices. There are a couple of
*applys (including mApply in Hmisc) but I haven't found
out how to do it straightforward.
Applying to row indices works, but looks like a poor hack
to me:
sens <- function(test, gold) {
if (any(gold==1)) {
sum(test[which(gold==1)]/sum(which(gold==1)))
} else NA
}
2007 Aug 10
1
[Fwd: Re: How to apply functions over rows of multiple matrices]
[Apologies to Gabor, who I sent a personal copy of the reply
erroneously instead of posting to List directly]
[...]
> Perhaps what you really intend is to
> take the average over those elements in each row of the first matrix
which correspond to 1's in the second in the corresponding
> row of the second. In that case its just:
>
> rowSums(newtest * goldstandard) /
2018 Mar 21
5
Sum of columns of a data frame equal to NA when all the elements are NA
Dear list users,
let me ask you this trivial question. I worked on that for a long time, by now.
Suppose to have a data frame with NAs and to sum some columns with rowSums:
df <- data.frame(A = runif(10), B = runif(10), C = rnorm(10))
df[1, ] <- NA
rowSums(df[ , which(names(df) %in% c("A","B"))], na.rm=T)
If all the elements of the selected columns are NA, rowSums
2017 Apr 01
3
mean(x) != mean(rev(x)) different with x <- c(NA, NaN) for some builds
In R 3.3.3, I observe the following on Ubuntu 16.04 (when building
from source as well as for the sudo apt r-base build):
> x <- c(NA, NaN)
> mean(x)
[1] NA
> mean(rev(x))
[1] NaN
> rowMeans(matrix(x, nrow = 1, ncol = 2))
[1] NA
> rowMeans(matrix(rev(x), nrow = 1, ncol = 2))
[1] NaN
> .rowMeans(x, m = 1, n = 2)
[1] NA
> .rowMeans(rev(x), m = 1, n = 2)
[1] NaN
>
2018 Mar 21
3
Sum of columns of a data frame equal to NA when all the elements are NA
What do you mean by "should not"?
NULL means "missing object" in R. The result of the sum function is always expected to be numeric... so NA_real or NA_integer could make sense as possible return values. But you cannot compute on NULL so no, that doesn't work.
See the note under the "Value" section of ?sum as to why zero is returned when all inputs are removed.
2018 Mar 21
0
Sum of columns of a data frame equal to NA when all the elements are NA
Should not the result be NULL if you have removed the NA with na.rm=TRUE ?
B.
> On Mar 21, 2018, at 11:44 AM, Stefano Sofia <stefano.sofia at regione.marche.it> wrote:
>
> Dear list users,
> let me ask you this trivial question. I worked on that for a long time, by now.
> Suppose to have a data frame with NAs and to sum some columns with rowSums:
>
> df <-
2018 Mar 21
0
Sum of columns of a data frame equal to NA when all the elements are NA
On 21/03/2018 11:44 AM, Stefano Sofia wrote:
> Dear list users,
> let me ask you this trivial question. I worked on that for a long time, by now.
> Suppose to have a data frame with NAs and to sum some columns with rowSums:
>
> df <- data.frame(A = runif(10), B = runif(10), C = rnorm(10))
> df[1, ] <- NA
> rowSums(df[ , which(names(df) %in%
2018 Mar 21
2
Sum of columns of a data frame equal to NA when all the elements are NA
No. The empty sum is zero. Adding it to another sum should not change it. Nothing audacious about that. This is consistent; other definitions just cause trouble.
-pd
> On 21 Mar 2018, at 18:05 , Boris Steipe <boris.steipe at utoronto.ca> wrote:
>
> Surely the result of summation of non-existent values is not defined, is it not? And since the NA values have been _removed_,
2018 Mar 21
0
Sum of columns of a data frame equal to NA when all the elements are NA
Surely the result of summation of non-existent values is not defined, is it not? And since the NA values have been _removed_, there's nothing left to sum over. In fact, pretending the the result in that case is zero would appear audacious, no?
Cheers,
Boris
> On Mar 21, 2018, at 12:58 PM, Jeff Newmiller <jdnewmil at dcn.davis.ca.us> wrote:
>
> What do you mean by
2012 Jan 10
4
Sum of a couple of variables of which a few have NA values
Dear everyone,
I have looked all over the internet but I cannot find a way to solve my problem.
In my data I want to sum a couple of variables. Some of these
variables have NA values, and when I add them together, the result is
NA
dat <- data.frame(
id = gl(5,1),
var1 = rnorm(5, 10),
var2 = rnorm(5, 7),
var3 = rnorm(5, 6),
var4 = rnorm(5, 3),
var5 = rnorm(5, 8)
)
dat[3,3] <- NA
dat[4,5]
2018 Mar 21
0
Sum of columns of a data frame equal to NA when all the elements are NA
I see: consistency with additive identity. That makes sense. Thanks.
B.
> On Mar 21, 2018, at 1:22 PM, peter dalgaard <pdalgd at gmail.com> wrote:
>
> No. The empty sum is zero. Adding it to another sum should not change it. Nothing audacious about that. This is consistent; other definitions just cause trouble.
>
> -pd
>
>> On 21 Mar 2018, at 18:05 , Boris
2017 Apr 01
1
mean(x) != mean(rev(x)) different with x <- c(NA, NaN) for some builds
On Fri, Mar 31, 2017 at 10:14 PM, Prof Brian Ripley
<ripley at stats.ox.ac.uk> wrote:
> From ?NA
>
> Numerical computations using ?NA? will normally result in ?NA?: a
> possible exception is where ?NaN? is also involved, in which case
> either might result.
>
> and ?NaN
>
> Computations involving ?NaN? will return ?NaN? or perhaps ?NA?:
>
2008 Feb 15
2
Remove rows with NA across all columns
Hi
I have a data frame df with 3 columns. Some rows are NA across all 3 columns. How can I remove rows with NA across all columns?
df=data.frame(col1=c(1:3,NA,NA,4),col2=c(7:9,NA,NA,NA),col3=c(2:4,NA,NA,4))
Thanks
Joseph
____________________________________________________________________________________
Be a better friend, newshound, and
[[alternative HTML version deleted]]
2018 Mar 21
1
Sum of columns of a data frame equal to NA when all the elements are NA
"I see: consistency with additive identity. "
Ummm, well:
> 1+NULL
numeric(0)
> sum(1,NULL)
[1] 1
Of course, there could well be something here I don't get, but that doesn't
look very consistent to me. However, as I said privately, so long as the
corner case behavior is documented, which it is, I don't care.
Cheers,
Bert
Bert Gunter
"The trouble with
2009 Jan 13
2
NA-values and logical operation
Dear list,
as a result of a logical operation I want to assign
a new variable to a DF with NA-values.
z <- data.frame( x = c(5,6,5,NA,7,5,4,NA),
y = c(1,2,2,2,2,2,2,2) )
p <- (z$x <= 5) & (z$y == 1)
p
z[p, "p1"] <-5
z
# ok, this works fine
z <- z[,-3]
p <- (z$x <= 5) & (z$y == 2)
p
z[p, "p2"] <-5
z
# this failed... - how
2006 Mar 14
1
ignoring objects containing NaN's in a recursive function
Hi-
General question:
I have written a function that performs a permutation test. I use the
function "sample" to randomize my data, then I call on another function
I wrote to analyze the randomized data. This process repeats "n"
times-- 10, 100 or whatever.
The function applied to the randomized data creates a matrix and
sometimes the matrix contains NaN's. I would
2010 May 25
1
Assigning NA to a rows of a dataframe/datamatrix
Dear R-users, I have a problem, I have the following dataframe:
d<-data.frame(
'y1'=c(1,2,1,2,1,NA,NA),
'y2'=c(1,2,1,1,1,2,1),
'y3'=c(1,NA,1,NA,NA,2,1),
'y4'=c(NA,2,NA,1,1,2,NA),
'a'=c(1,1,1,1,1,1,2)
)
where the last variable counts the number of missing values in a row. Now, i want to set rows where a>1 to NA and arrive at something like the