search for: mydf

Displaying 20 results from an estimated 291 matches for "mydf".

2011 May 19
1
Creating a "shifted" month (one that starts not on the first of each month but on another date)
...arts 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: mydf<-data.frame(mydate=seq(as.Date("2011-01-01"), length = 92, by = "day")) (mydf) ### Creating a new variable that has one value before ### the 20th of each month and next value after it mydf$daynum<-as.numeric(format(mydate,"%d")) library(zoo) mydf$yearmon<-as...
2017 Nov 22
6
assign NA to rows by test on multiple columns of a data frame
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) mydf<-data.frame(A, A_flag, B, B_flag) # this is my initial df mydf I want to get to this final situation i<-which(mydf$A_flag==0) mydf$A[i]<-NA ii<-which(mydf$B_flag==0) mydf$B[ii]<-NA # this is my final df mydf By considering that I have to perform...
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"]) (modulo exclusion of NAs) but use of the former yields an error from density.default() (shown below). Is...
2005 Dec 08
3
Reshaping data
Dear all, given I have data in a data.frame which indicate the number of people in a specific year at a specific age: n <- 10 mydf <- data.frame(yr=sample(1:10, size=n, replace=FALSE), age=sample(1:12, size=n, replace=FALSE), no=sample(1:10, size=n, replace=FALSE)) Now I would like to make a matrix with (in this simple example) 10 columns (for the years) and 12 rows (for the ages). In...
2005 Feb 03
2
Surprising Behavior of 'tapply'
...ounting 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 <- as.data.frame(cbind(sex, income, count)) > mydf$count = as.numeric(as.character(mydf$count)) > tapply(mydf$count, list(mydf$sex, mydf$income), FUN=sum) high low F 16 9 M 24 6 > version _ platform i386-pc-mingw32 arch i386 os mingw...
2007 Sep 01
2
Comparing "transform" to "with"
...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) x4<-c(5,6,9) myDF<-data.frame(x1,x2,x3,x4) rm(x1,x2,x3,x4) ls() myDF This creates two new variables just fine" transform(myDF, sum1=x1+x2, sum2=x3+x4 ) This next code does not see sum1, so it appears that "transform" cannot see the variables that it creates. Would I need to transform new var...
2011 May 04
0
Fwd: simple question
...use me: #example data, the real data consists of 20000 pairs of variables K1 <- c(1,2,1, 1, 1,1); K2 <- c(1, 1,2,2, 1,2); K3 <- c(3, 1, 3, 3, 1, 3) M1a <- rep( K1, 100); M1b <- rep(K2, 100) M2a <- rep(K1, 100); M2b <- rep(K1, 100) M3a <- rep(K1, 100); M3b <- rep(K3, 100) mydf <- data.frame(M1a, M1b, M2a, M2b, M3a, M3b) # matrix operation nmat <- matrix(c(paste('M', 1:3, 'a', sep = ''), paste('M', 1:3, 'b', sep = '')), 3) coffin <- function(x) { x <- as.vector(x) d1cf <- ifelse(mydf[x[1]] == mydf[x[2]],0,...
2011 Jun 09
1
Error: missing values where TRUE/FALSE needed
...ix = c("cheap", "budget"), prefix2 = c("low cost"), suffix = c("quote", "quotes"), suffix2 = c("rate", "rates"), suffix3 = c("comparison")) myone <- function(x, y) { nu <- do.call(paste, expand.grid(lst$x, lst$y)) mydf <- data.frame(keyword=c(nu)) } mytwo <- function(x, y, z){ mu <- do.call(paste, expand.grid(lst$x, lst$y, lst$z)) mydf2 <- data.frame(keyword=c(mu)) } d1 = mytwo(lst$prefix, lst$roots, lst$suffix) d2 = mytwo(lst$prefix, lst$roots, lst$suffix2) d3 = mytwo(lst$prefix, lst$roots, lst$suffi...
2017 Nov 22
1
assign NA to rows by test on multiple columns of a data frame
...ot; <massimo.bressan at arpa.veneto.it>, "r-help" <r-help at r-project.org> Inviato: Mercoled?, 22 novembre 2017 11:49:08 Oggetto: Re: [R] 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, M...
2013 Feb 18
1
ggplot2 and facet_wrap help
....frame(price=matrix(sample(1:1000, 100, replace = TRUE), ncol = 1)) df$size1 = 1:nrow(df) df$size1 = cut(df$size1, breaks=11) df=df[sample(nrow(df)),] df$size2 = 1:nrow(df) df$size2 = cut(df$size2, breaks=11) df=df[sample(nrow(df)),] df$clarity = 1:nrow(df) df$clarity = cut(df$clarity, breaks=6) mydf = aggregate(df$price, by=list(df$size1, df$size2, df$clarity),median) names(mydf)[1] = 'size1' names(mydf)[2] = 'size2' names(mydf)[3] = 'clarity' names(mydf)[4] = 'median_price' # So my data is already in a "long" format I think, but when I do this: ggp...
2013 Feb 26
2
merging or joining 2 dataframes: merge, rbind.fill, etc.?
#I want to "merge" or "join" 2 dataframes (df1 & df2) into a 3rd (mydf). I want the 3rd dataframe to contain 1 row for each row in df1 & df2, and all the columns in both df1 & df2. The solution should "work" even if the 2 dataframes are identical, and even if the 2 dataframes do not have the same column names. The rbind.fill function seems to work....
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, M...
2007 Sep 02
2
NAs in indices
...points out the problem and solution but not why they differ. A simplified program that demonstrates the issue is below. Thanks, Bob # Here's a data frame that has both periods and NAs. # I want sex to remain character for now. sex=c("m","f",".",NA) x=c(1,2,3,NA) myDF <- data.frame(sex,x,stringsAsFactors=F) rm(sex,x) myDF # Substituting NA into data frame does not work # due to NAs in the indices. The error message is: # missing values are not allowed in subscripted assignments of data frames myDF[ myDF$sex==".", "sex" ] <- NA myDF #...
2011 Aug 16
1
Utilizing column names to multiply over all columns
## Hello there, ## I have an issue where I need to use the value of column names to multiply with the individual values in a column and I have many columns to do this over. I have data like this where the column names are numbers: mydf <- data.frame(`2.72`=runif(20, 0, 125), `3.2`=runif(20, 50, 75), `3.78`=runif(20, 0, 100), yy= head(letters,2), check.names=FALSE) ## I had been doing something like this but this seems rather tedious and clunky. These append the correct val...
2011 Jun 14
1
Invalid Regular Expression
...ompany_match <- str_c(inscompany, collapse = "|") state_match <- str_c(state, collapse = "|") city_match <- str_c(city, collapse = "|") agency_match <- str_c(agency, collapse = "|") zipcode_match <- str_c(zipcode, collapse = "|") mydf$inscompany <- as.numeric(str_detect(mydf$keyword, inscompany_match)) mydf$state <- as.numeric(str_detect(mydf$keyword, state_match)) mydf$city <- as.numeric(str_detect(mydf$keyword, city_match)) mydf$agency <- as.numeric(str_detect(mydf$keyword, agency_match)) mydf$zipcode <- as....
2008 Jan 03
2
retaining formatting when converting a vector to a matrix/data.frame?
Please see example code below. I have a vector ("mydata") of length 10. "mydata" can have various formats (e.g. numeric, text, POSIXct, etc) I use the matrix and data.frame functions to convert "mydata" to a dataframe ("mydf") of 2 columns and 5 rows. What is a "good" way to ensure that the format is retained when I create the data.frame? Currently, data in the "POSIXct" format is converted to numeric. Also, for my edification, is there a "good" way to convert numeric va...
2017 Nov 22
1
assign NA to rows by test on multiple columns of a data frame
...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 < > massimo.bressan at arpa.veneto.it> wrote: > >> >> >> Given this data frame (a simplified, essential reproducible example) >> >> >> >> >> A<-c(8,7,10...
2017 Nov 23
1
assign NA to rows by test on multiple columns of a data frame
...4567 at gmail.com> A: "Massimo Bressan" <massimo.bressan at arpa.veneto.it> Cc: "r-help" <r-help at r-project.org> Inviato: Mercoled?, 22 novembre 2017 17:32:33 Oggetto: Re: [R] 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 ope...
2001 Nov 05
3
vector problems
I dont get it: > is.vector(c(mydf[1])) [1] TRUE > unique(c(mydf[1])) Error in unique(c(mydf[1])) : unique() applies only to vectors > Is it a vector or not? This stuff is driving me nuts. I'm simply trying to convince R that my grouping vector is actually a vector so that unique will work. Its just a vector of numbers,...
2023 Nov 04
2
I need to create new variables based on two numeric variables and one dichotomize conditional category variables.
...ld in any way be quicker. But might be to some extent easier to develop variations of. And is sort of what factors should be doing... # make dummy data gender <- c("Male", "Female", "Male", "Female") WC <- c(70,60,75,65) TG <- c(0.9, 1.1, 1.2, 1.0) myDf <- data.frame( gender, WC, TG ) # label a factor myDf$GF <- factor(myDf$gender, labels= c("Male"=65, "Female"=58)) # do the maths myDf$LAP <- (myDf$WC - as.numeric(myDf$GF))* myDf$TG #show results head(myDf) gender WC TG GF LAP 1 Male 70 0.9 58 61.2 2 Female 60...