Dear Listers, I try to change the structure of my data. i have an indicator-matrix and want to end up with a factor. i have v1 v2 v3 1 0 0 0 1 0 0 0 1 and want v1 v2 v3 v4 1 0 0 1 0 1 0 2 0 0 1 3 amongst other things i tried the following d <- data.frame(d1=c(1,0,0), d2=c(0,1,0), d3=c(0,0,1)) d$nr <- NA sapply(1:3, function(x) ifelse(get(paste0("d$d",x))==1,x,d$nr))>From R i get the message "Object 'd$d1' not found".But why, it's there? Thanks a lot. Winfried [[alternative HTML version deleted]]
try this:> x <- read.table(text = "v1 v2 v3+ 1 0 0 + 0 1 0 + 0 0 1", header = TRUE)> > x$v4 <- apply(x, 1, function(a) which(a == 1)) > xv1 v2 v3 v4 1 1 0 0 1 2 0 1 0 2 3 0 0 1 3> > >On Thu, Feb 7, 2013 at 1:20 PM, Winfried Moser <winfried.moser at gmail.com> wrote:> Dear Listers, > > I try to change the structure of my data. i have an indicator-matrix and > want to end up with a factor. > > i have > > v1 v2 v3 > 1 0 0 > 0 1 0 > 0 0 1 > > and want > > v1 v2 v3 v4 > 1 0 0 1 > 0 1 0 2 > 0 0 1 3 > > amongst other things i tried the following > > d <- data.frame(d1=c(1,0,0), d2=c(0,1,0), d3=c(0,0,1)) > d$nr <- NA > sapply(1:3, function(x) ifelse(get(paste0("d$d",x))==1,x,d$nr)) > > >From R i get the message "Object 'd$d1' not found". > But why, it's there? > > Thanks a lot. > Winfried > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
On 07/02/2013 1:20 PM, Winfried Moser wrote:> Dear Listers, > > I try to change the structure of my data. i have an indicator-matrix and > want to end up with a factor. > > i have > > v1 v2 v3 > 1 0 0 > 0 1 0 > 0 0 1 > > and want > > v1 v2 v3 v4 > 1 0 0 1 > 0 1 0 2 > 0 0 1 3 > > amongst other things i tried the following > > d <- data.frame(d1=c(1,0,0), d2=c(0,1,0), d3=c(0,0,1)) > d$nr <- NA > sapply(1:3, function(x) ifelse(get(paste0("d$d",x))==1,x,d$nr)) > > >From R i get the message "Object 'd$d1' not found". > But why, it's there?No it's not there. There's a variable called d, but no variable called d$d1. That's an expression to extract the d1 component from d. You could parse and eval that expression, but surely it's not the right way to do this. In your simple example, using d[[x]] would work. In a more complicated one, you might want something like components <- names(d) d[[components[x]]] Duncan Murdoch
Hi, You could get the result: dat1 <- read.table(text = " v1 v2 v3 ?1?? 0?? 0 ?0?? 1?? 0 ?0?? 0?? 1 ",sep="",header=TRUE) dat1$v4<-apply(mapply(`==`,dat1,1),2,which) ?dat1 ?# v1 v2 v3 v4 #1? 1? 0? 0? 1 #2? 0? 1? 0? 2 #3? 0? 0? 1? 3 A.K. ----- Original Message ----- From: Winfried Moser <winfried.moser at gmail.com> To: r-help <r-help at r-project.org> Cc: Sent: Thursday, February 7, 2013 12:20 PM Subject: [R] why "object 'x' not found"? Dear Listers, I try to change the structure of my data. i have an indicator-matrix and want to end up with a factor. i have v1 v2 v3 1? 0? 0 0? 1? 0 0? 0? 1 and want v1 v2 v3? v4 1? 0? 0? 1 0? 1? 0? 2 0? 0? 1? 3 amongst other things i tried the following d <- data.frame(d1=c(1,0,0), d2=c(0,1,0), d3=c(0,0,1)) d$nr <- NA sapply(1:3, function(x) ifelse(get(paste0("d$d",x))==1,x,d$nr))>From R i get the message "Object 'd$d1' not found".But why, it's there? Thanks a lot. Winfried ??? [[alternative HTML version deleted]] ______________________________________________ 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.
On Feb 7, 2013, at 10:20 AM, Winfried Moser wrote:> Dear Listers, > > I try to change the structure of my data. i have an indicator-matrix and > want to end up with a factor. > > i have > > v1 v2 v3 > 1 0 0 > 0 1 0 > 0 0 1 > > and want > > v1 v2 v3 v4 > 1 0 0 1 > 0 1 0 2 > 0 0 1 3 > > amongst other things i tried the following > > d <- data.frame(d1=c(1,0,0), d2=c(0,1,0), d3=c(0,0,1)) > d$nr <- NA > sapply(1:3, function(x) ifelse(get(paste0("d$d",x))==1,x,d$nr)) > >> From R i get the message "Object 'd$d1' not found". > But why, it's there?No, it's not "there". There is nothing with that name. At the console `d$d1` is a function being applied to 'd', whereas the `get` function expects a character argument or something that evaluate to a character. This is somewhat similar to what you were attempting and syntactically succeeds:> sapply(1:3, function(x) ifelse(d[[ paste0("d",x) ]]==1,x,d$nr))[,1] [,2] [,3] [1,] 1 NA NA [2,] NA 2 NA [3,] NA NA 3 Notice that the '[[' function is superior in every way to the '$' function. -- David.
Maybe Matching Threads
- use name (not values!) of a dataframe inside a funktion
- conditional Dataframe filling
- how can I import a number of datsets in a folder in my working directory to a list in R
- change default path for installing r-cran packages by sudo apt r-cran...?
- equivalent of group command of the egen function in Stata