similar to: assign NA to rows by test on multiple columns of a data frame

Displaying 20 results from an estimated 2000 matches similar to: "assign NA to rows by test on multiple columns of a data frame"

2017 Nov 22
0
assign NA to rows by test on multiple columns of a data frame
Hello, Try the following. icol <- which(grepl("flag", names(mydf))) mydf[icol] <- lapply(mydf[icol], function(x){ is.na(x) <- x == 0 x }) mydf # A A_flag B B_flag #1 8 10 5 12 #2 7 NA 6 9 #3 10 1 2 NA #4 1 NA 1 5 #5 5 2 0 NA Hope this helps, Rui Barradas On 11/22/2017 10:34 AM, Massimo Bressan
2017 Nov 22
1
assign NA to rows by test on multiple columns of a data frame
...well, I don't think this is exactly the expected result (see my post) to be noted that the columns affected should be "A" and "B" thanks for the help max ----- Messaggio originale ----- Da: "Rui Barradas" <ruipbarradas at sapo.pt> A: "Massimo Bressan" <massimo.bressan at arpa.veneto.it>, "r-help" <r-help at
2017 Nov 23
1
assign NA to rows by test on multiple columns of a data frame
yes, it works, even if I do not really get how and why it's working the combination of logical results (could you provide some insights for that?) moreover, and most of all, I was hoping for a compact solution because I need to deal with MANY columns (more than 40) in data frame with the same basic structure as the simplified example I posted thanks m ----- Messaggio originale ----- Da:
2017 Nov 22
0
assign NA to rows by test on multiple columns of a data frame
Do you mean like this: mydf <- within(mydf, { is.na(A)<- !A_flag is.na(B)<- !B_flag } ) > mydf A A_flag B B_flag 1 8 10 5 12 2 NA 0 6 9 3 10 1 NA 0 4 NA 0 1 5 5 5 2 NA 0 Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into
2017 Nov 22
0
assign NA to rows by test on multiple columns of a data frame
Hi *Massimo,* *Try this.* *a <- mydf==0mydf[a] <- NAHTHEK* On Wed, Nov 22, 2017 at 5:34 AM, Massimo Bressan < massimo.bressan at arpa.veneto.it> wrote: > > > Given this data frame (a simplified, essential reproducible example) > > > > > A<-c(8,7,10,1,5) > > A_flag<-c(10,0,1,0,2) > > B<-c(5,6,2,1,0) > > B_flag<-c(12,9,0,5,0) >
2017 Nov 22
1
assign NA to rows by test on multiple columns of a data frame
OPS, Sorry i did not read the post carfully. Mine will not work if you have zeros on columns A and B.. But you could modify it to work for specific columns i believe. EK On Wed, Nov 22, 2017 at 8:37 AM, Ek Esawi <esawiek at gmail.com> wrote: > Hi *Massimo,* > > *Try this.* > > *a <- mydf==0mydf[a] <- NAHTHEK* > > On Wed, Nov 22, 2017 at 5:34 AM, Massimo Bressan
2017 Nov 09
0
weighted average grouped by variables
Hello an update about my question: I worked out the following solution (with the package "dplyr") library(dplyr) mydf%>% mutate(speed_vehicles=n_vehicles*mydf$speed) %>% group_by(date_time,type) %>% summarise( sum_n_times_speed=sum(speed_vehicles), n_vehicles=sum(n_vehicles), vel=sum(speed_vehicles)/sum(n_vehicles) ) In fact I was hoping to manage everything in a
2017 Nov 09
4
weighted average grouped by variables
hi all I have this dataframe (created as a reproducible example) mydf<-structure(list(date_time = structure(c(1508238000, 1508238000, 1508238000, 1508238000, 1508238000, 1508238000, 1508238000), class = c("POSIXct", "POSIXt"), tzone = ""), direction = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c("A", "B"), class =
2017 Nov 09
2
weighted average grouped by variables
Hi Thanks for working example. you could use split/ lapply approach, however it is probably not much better than dplyr method. sapply(split(mydf, mydf$type), function(speed, n_vehicles) sum(mydf$speed*mydf$n_vehicles)/sum(mydf$n_vehicles)) gives you averages aggregate(mydf$n_vehicles, list(mydf$type), sum)$x gives you sums Cheers Petr > -----Original Message----- > From: R-help
2017 Nov 09
1
weighted average grouped by variables
Dear Massimo, It seems straightforward to use weighted.mean() in a dplyr context library(dplyr) mydf %>% group_by(date_time, type) %>% summarise(vel = weighted.mean(speed, n_vehicles)) Best regards, ir. Thierry Onkelinx Statisticus / Statistician Vlaamse Overheid / Government of Flanders INSTITUUT VOOR NATUUR- EN BOSONDERZOEK / RESEARCH INSTITUTE FOR NATURE AND FOREST Team
2017 Nov 11
0
weighted average grouped by variables
> On 9 Nov 2017, at 14:58, PIKAL Petr <petr.pikal at precheza.cz> wrote: > > Hi > > Thanks for working example. > > you could use split/ lapply approach, however it is probably not much better than dplyr method. > > sapply(split(mydf, mydf$type), function(speed, n_vehicles) sum(mydf$speed*mydf$n_vehicles)/sum(mydf$n_vehicles)) > gives you averages > The
2017 Nov 09
1
weighted average grouped by variables
Hello, Using base R only, the following seems to do what you want. with(mydf, ave(speed, date_time, type, FUN = weighted.mean, w = n_vehicles)) Hope this helps, Rui Barradas Em 09-11-2017 13:16, Massimo Bressan escreveu: > Hello > > an update about my question: I worked out the following solution (with the package "dplyr") > > library(dplyr) > > mydf%>% >
2017 Nov 09
0
weighted average grouped by variables
hi thierry thanks for your reply yes, you are right, your solution is more straightforward best Da: "Thierry Onkelinx" <thierry.onkelinx at inbo.be> A: "Massimo Bressan" <massimo.bressan at arpa.veneto.it> Cc: "r-help" <r-help at r-project.org> Inviato: Gioved?, 9 novembre 2017 15:17:31 Oggetto: Re: [R] weighted average grouped by
2018 Feb 25
0
reshaping column items into rows per unique ID
I believe you need to spend time with an R tutorial or two: a data frame (presumably the "table" data structure you describe) can *not* contain "blanks" -- all columns must be the same length, which means NA's are filled in as needed. Also, 8e^5 * 7e^4 = 5.6e^10, which almost certainly will not fit into any local version of R (maybe it would in some server version --
2018 Feb 25
4
reshaping column items into rows per unique ID
Hi All I have a datafram which looks like this : CustomerID DietType 1 a 1 c 1 b 2 f 2 a 3 j 4 c 4 c 4 f And I would like to reshape this so I can
2011 May 19
1
Creating a "shifted" month (one that starts not on the first of each month but on another date)
Hello! I have a data frame with dates. I need to create a new "month" that starts on the 20th of each month - because I'll need to aggregate my data later by that "shifted" month. I wrote the code below and it works. However, I was wondering if there is some ready-made function in some package - that makes it easier/more elegant? Thanks a lot! # Example data:
2017 Jul 05
4
expand gridded matrix to higher resolution
Hi all, (if me email goes out as html, than my email client don't do as told, and I apologies already.) We need to downscale climate data and therefore first need to expand the climate from 0.5deg to the higher resolution 10min, before we can add high resolution deviations. We basically need to have the original data at each gridcell replicated into 3x3 gridcells. A simple for loop can do
2009 Jan 20
5
Problem with subset() function?
Hi all, Can anyone explain why the following use of the subset() function produces a different outcome than the use of the "[" extractor? The subset() function as used in density(subset(mydf, ht >= 150.0 & wt <= 150.0, select = c(age))) appears to me from documentation to be equivalent to density(mydf[mydf$ht >= 150.0 & mydf$wt <= 150.0, "age"])
2007 Sep 01
2
Comparing "transform" to "with"
Hi All, I've been successfully using the with function for analyses and the transform function for multiple transformations. Then I thought, why not use "with" for both? I ran into problems & couldn't figure them out from help files or books. So I created a simplified version of what I'm doing: rm( list=ls() ) x1<-c(1,3,3) x2<-c(3,2,1) x3<-c(2,5,2)
2005 Feb 03
2
Surprising Behavior of 'tapply'
Dear all, I wanted to make a two-way-table of two variables with a counting variable stored in another column of a dataframe. In version 1.9.1, the behavior is as expected as shown in the simplified example code. > sex <- rep(c("F", "M"), 5) > income <- c(rep("low", 5), rep("high", 5)) > count <- 1:10 > mydf <-