I am using the following command to replace all the missing values and assorted typos in a dataframe with NA: mydata[mydata>80]=NA The problem is that the first column contains values which should be more than 80, so really I want to do it just for mydata[,2:length(mydata)] I can't seem to re-write the code to fit: mydata[,2:length(mydata)>80]=NA # no error message, but doesn't work- doesn't do anything, it would seem I realise I can just keep the first column somewhere safe and copy it back again when I'm done, but I wondered if there was a more elegant solution, which would be much more important, if say I just wanted to replace the odd columns, or something like that. I found this code on the internet too: idx <- which(foo>80, arr.ind=TRUE) foo[idx[1], idx[2]] <- NA But I can't seem to rewrite that either, for the same reason Many thanks! Chris Beeley Institute of Mental Health
Hi> > I am using the following command to replace all the missing values and > assorted typos in a dataframe with NA: > > mydata[mydata>80]=NA > > The problem is that the first column contains values which should be > more than 80, so really I want to do it just for > mydata[,2:length(mydata)] > > I can't seem to re-write the code to fit: > > mydata[,2:length(mydata)>80]=NA # no error message, but doesn't work- > doesn't do anything, it would seemmydata[,-1][mydata[,-1]>80]<-NA shall do what you want Regards Petr> > I realise I can just keep the first column somewhere safe and copy it > back again when I'm done, but I wondered if there was a more elegant > solution, which would be much more important, if say I just wanted to > replace the odd columns, or something like that. > > I found this code on the internet too: > > idx <- which(foo>80, arr.ind=TRUE) > foo[idx[1], idx[2]] <- NA > > But I can't seem to rewrite that either, for the same reason > > Many thanks! > > Chris Beeley > Institute of Mental Health > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Dimitris Rizopoulos
2011-Jun-20 09:00 UTC
[R] Replace selected columns of a dataframe with NA
I don't know if you have factors in your data frame, but in any case,
you can try the following approach:
mydata <- data.frame(id = 1:10, x = rnorm(10, 80),
z = rnorm(10, 80), w = gl(2, 5))
mydata
f <- function (x) {
if (is.numeric(x))
x[x > 80] <- NA
x
}
mydata[-1] <- lapply(mydata[-1], f)
mydata
I hope it helps.
Best,
Dimitris
On 6/20/2011 10:40 AM, Chris Beeley wrote:> I am using the following command to replace all the missing values and
> assorted typos in a dataframe with NA:
>
> mydata[mydata>80]=NA
>
> The problem is that the first column contains values which should be
> more than 80, so really I want to do it just for
> mydata[,2:length(mydata)]
>
> I can't seem to re-write the code to fit:
>
> mydata[,2:length(mydata)>80]=NA # no error message, but doesn't
work-
> doesn't do anything, it would seem
>
> I realise I can just keep the first column somewhere safe and copy it
> back again when I'm done, but I wondered if there was a more elegant
> solution, which would be much more important, if say I just wanted to
> replace the odd columns, or something like that.
>
> I found this code on the internet too:
>
> idx<- which(foo>80, arr.ind=TRUE)
> foo[idx[1], idx[2]]<- NA
>
> But I can't seem to rewrite that either, for the same reason
>
> Many thanks!
>
> Chris Beeley
> Institute of Mental Health
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>
--
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center
Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014
Web: http://www.erasmusmc.nl/biostatistiek/
Apparently Analagous Threads
- Match strings across two differently sized dataframes and copy corresponding row to dataframe
- Subset command and the : operator
- as.numeric() generates NAs inside an apply call, but fine outside of it
- Basic question about re-writing for loop as a function
- Some coefficients are doubled when I use the step() function