similar to: replace NA-values

Displaying 20 results from an estimated 20000 matches similar to: "replace NA-values"

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 Aug 03
2
efficient way to make NAs of empty cells in a factor (or character)
Dear all, I have some csv-files (originating from Excel-files) containing empty cells. In my example file I have four variables of different classes, each with some empty cells in the original csv-file: > test <- read.csv2("test.csv", dec=".") > test id id2 x y 1 a 1 NA 2 b e NA 2.2 3 f 3 3.3 4 c g 4 4.4 > class(test$id) [1]
2009 Mar 02
2
ave and grouping
Dear list, # I have a DF like this: sleep$b <- c(rep(8,10), rep(9,10)) sleep$me <- with(sleep, ave(extra, group, FUN = mean)) sleep # I would like to create a new variable # holding the b-th value of group 1 and 2. # This is not what I want, it takes always the '8' from group '1' # and not the '9' sleep$gr <- with(sleep, ave(extra, group, FUN = function(x)
2002 Jul 03
2
Adding text to a plot
Dear R-users, again two question... # Question 1 Adding two lines of text to a plot, I am using: # ------------------------------- plot(k[,1], k[,2], pch=16, ylim=range((min(k[,2])-0.2):(max(k[,2])+1))) a <- paste("Cor.:" ,cor(k[,2],k[,1])) b <- paste(nrow(k), "Countries") text(90, max((k[,2])-0.51), a) text(90, max((k[,2])-0.83), b) #
2014 Jan 19
5
how to replace <NA> values
Dear R community   I have a large data set contain some empty cells. Because of that,  may be I am wrong, <NA> values are produced. Now I want replace both empty and <NA> values with zero.   Elder1 <- data.frame(   ID=c("ID1","ID2","ID3","ID6","ID8"),   age=c(38,35,"",NA,NA)) Output I am expecting   ID   age ID1  38 ID2  35
2008 Dec 15
5
how to create duplicated ID in multi-records per subject dataset
Hi R helpers, If I have a dataset looks like: ID record 1 20 . 30 . 25 2 26 . 15 3 21 4..................... And I want it becomes ID record 1 20 1 30 1 25 2 26 2 15 3 21 4..................... That is, I have to duplicate IDs for those with multiple records. I am wondering it is possible to be
2002 Jun 14
1
data.frame - transform
Hi there, I have a data.frame (pwt6) which I would like to transform: country year gdp MEX 1950 2 MEX 1951 5 BOL 1950 4 BOL 1951 12 ITA 1950 45 ITA 1951 2 This should be the result: year MEX.gdp BOL.gdp ITA.gdp 1950 2 4 45 1951 5 12 2 Right now I have this code (better - no code): country.label<-names(table(pwt6$country)) result<-data.frame(year=NULL) for(i in country.label) ?
2002 Jun 09
1
Logical operators
Hello, I have a csv-file which looks like: #### pwt6_r.csv #### code;year;rgdpch AGO;1998;1234 ALB;1998;3576 ARG;1998;#NA SVN;1996;13439 SWE;1996;21492 AGO;1960;#NA ALB;1960;2345 ARG;1960;4634 #### pwt6_r.csv #### To import this file i call: pwt6<-read.csv("d:/pwt6_r.csv",header=T,na.strings="#NA",sep=";") Now I want to generate a new data.frame which include
2002 Jun 06
3
Problem with get.hist.quote (tseries library)....
Hello, I am having a problem with the get.hist.quote command (tseries library) in the Windows version. This problem is not happening is the Linux version (Mandrake 8.2). Attached is the error message, for an example included in the help file. Also the R.Version() details is attached. Please, do you know if there is a workaround ? Thanks, Carlos. ++++++++++++++++++++++++ ERROR MESSAGE
2013 Apr 12
1
Removing rows that are duplicates but column values are in reversed order
Hi, From your example data, dat1<- read.table(text=" id1?? id2?? value a????? b?????? 10 c????? d??????? 11 b???? a???????? 10 c????? e???????? 12 ",sep="",header=TRUE,stringsAsFactors=FALSE) #it is easier to get the output you wanted dat1[!duplicated(dat1$value),] #? id1 id2 value #1?? a?? b??? 10 #2?? c?? d??? 11 #4?? c?? e??? 12 But, if you have cases like the one
2008 Nov 20
2
how to replace NA with previous numbers
Hi, I have a vector with lots of NAs. e.g. vec = c(NA, NA, 2, NA, NA, 5, NA, 6, NA) > vec [1] NA NA 2 NA NA 5 NA 6 NA I would like to replace NAs with their immediately previous non NA number. After replacement, the above vector will become > vec [1] 0 0 2 2 2 5 5 6 6. I understand how to do that with a loop but the actual vector is very long and the loop takes too much time in R.
2005 Aug 10
1
Why only a "" string for heading for row.names with write.csv with a matrix?
Consider: > x <- matrix(1:6, 2,3) > rownames(x) <- c("ID1", "ID2") > colnames(x) <- c("Attr1", "Attr2", "Attr3") > x Attr1 Attr2 Attr3 ID1 1 3 5 ID2 2 4 6 > write.csv(x,file="x.csv") "","Attr1","Attr2","Attr3" "ID1",1,3,5
2012 Nov 22
2
Trick to replace NA
Dear members, I have a series of values in a vector and some value are missing and replaced with NA. For example: a <- c(27, 25, NA, NA, 24, 26, 27, NA, 26) I would like to replace the NAs with the value taken from the previous value that is non-NA. The output would be in this case: 27 25 25 25 24 26 27 27 26 Now I do that with a for loop, but I try to eliminate all the loops to gain in
2011 Feb 23
2
list multiplied by a factor / mapply
Dear list, this works fine: x <- split(iris, iris$Species) x1 <- lapply(x, function(L) transform(L, g = L[,1:4] * 3)) but I would like to multiply each Species with another factor: setosa by 2, versicolor by 3 and virginica by 4. I've tried mapply but without success. Any thoughts? Thanks for any idea! Patrick
2010 Apr 29
3
Simple loop code
Hi fellow R Users, I find that I typically rewrite my data specific to data in columns, which is by no means efficient and I am struggling to break out of this bad habit and utalise some of the excellent things R can do! I have tried to look at 'for' but I don't really follow it, and I wondered if anyone could help with a simple example using my script so I could follow this and build
2011 Mar 24
1
fraction with timelag
Dear r-help, I'm having this DF df <- data.frame(id = 1:6, xout = c(1234, 2134, 234, 456, 324, 345), xin= c(NA, 34,67,87,34, NA)) and would like to calculate the fraction (xin_t / xout_t-1) The result should be: # NA, 2.76, 3.14, 37.18, 7.46, NA I am sure there is a solution using zoo... but I don't know how... Thanks for any help! Patrick
2008 Aug 30
1
strsplit and regexp
Dear list, I am trying to split a string using regexp: x <- "2 Value 34 a-c 45 t" strsplit(x, "[0-9]") [[1]] [1] "" " Value " "" " a-c " "" " t" But I don't want to lose the digits (pattern), the result should be: [[1]] [1] "2" " Value " "34" " a-c "
2011 Jan 10
1
Using combn
Dear list, I want to apply the "table" function to every pair of variables in df and the return should be a list. setwd(123) asd <- data.frame(a1=sample(1:4, 20, replace=TRUE), a2=sample(1:4, 20, replace=TRUE), a3=sample(1:4, 20, replace=TRUE), a4=sample(1:4, 20, replace=TRUE)) with(asd, table(a1, a2)) with(asd, table(a1,
2018 Jan 08
2
Replace NAs in split lists
You can enforce these assumptions by sorting on multiple columns, which leads to na.locf(df1[ order(df1$ID,df1$Value), ]) On Mon, Jan 8, 2018 at 4:19 PM, Jeff Newmiller <jdnewmil at dcn.davis.ca.us> wrote: > Yes, you are right if the IDs are always sequentially-adjacent and the > first non-NA value appears in the first record for each ID. > -- > Sent from my phone. Please
2008 May 17
1
tapply and grouping
Hello all, I have a df like this: w <- c(1.20, 1.34, 2.34, 3.12, 2.89, 4.67, 2.43, 2.89, 1.99, 3.45, 2.01, 2.23, 1.45, 1.59) g <- rep(c("a", "b"), each=7) df <- data.frame(g, w) df # 1. Mean for each group tapply(df$w, df$g, function(x) mean(x)) # 2. Range for each group - fix value 0.15 tapply(df$w, df$g, function(x) x[(x > mean(x) -