Hi all, I have a dataframe (df1) that I am trying to select values from to a second dataframe that at the current time is only for the selected items from df1 (df2). The values that I am trying to save from df1 are factors with alphanumeric names df1 looks like this: 'data.frame': 3014 obs. of 13 variables: $ Num : int 1 1 1 2 2 2 3 3 3 4 ... $ Tag_Num : int 1195 1195 1195 1162 1162 1162 1106 1106 1106 1173 ... $ Site : Factor w/ 25 levels "PYBR002A","PYBR003B",..: 1 1 1 1 1 1 1 1 1 1 ... $ Site_IndNum : Factor w/ 1044 levels "PYBR002A_001",..: 1 1 1 2 2 2 3 3 3 4 ... .... ... $ Area : num 463.3 29.5 101.8 152.9 34.6 ... However, whenever I try to assign values, like this df2[j,1]<-df2$Site[i] the values are changed from alphanumeric (e.g. PYBR003A) to numerals (e.g. 1). Does anyone know why this is happening and how I can assign the actual values from df1 to df2? Thanks in advance, Wade [[alternative HTML version deleted]]
Could you give a small reproducible example please? It is not clear to me what your looping structure is doing, or what your goal here is. There may be a much simpler method than introducing subscripts. --Erik Wade Wall wrote:> Hi all, > > I have a dataframe (df1) that I am trying to select values from to a second > dataframe that at the current time is only for the selected items from df1 > (df2). The values that I am trying to save from df1 are factors with > alphanumeric names > > df1 looks like this: > > 'data.frame': 3014 obs. of 13 variables: > $ Num : int 1 1 1 2 2 2 3 3 3 4 ... > $ Tag_Num : int 1195 1195 1195 1162 1162 1162 1106 1106 1106 1173 ... > $ Site : Factor w/ 25 levels "PYBR002A","PYBR003B",..: 1 1 1 1 1 1 1 > 1 1 1 ... > $ Site_IndNum : Factor w/ 1044 levels "PYBR002A_001",..: 1 1 1 2 2 2 3 3 3 > 4 ... > .... ... > $ Area : num 463.3 29.5 101.8 152.9 34.6 ... > > However, whenever I try to assign values, like this > > df2[j,1]<-df2$Site[i] > > the values are changed from alphanumeric (e.g. PYBR003A) to numerals (e.g. > 1). > > Does anyone know why this is happening and how I can assign the actual > values from df1 to df2? > > Thanks in advance, > > Wade > > [[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.
Jorge Ivan Velez
2010-Nov-05 20:10 UTC
[R] assignment operator saving factor level as number
Hi Wade, Try (untested): df2[j,1] <- as.character(f2$Site)[i] If that does not work, which is very likely, could you please provide commented, minimal, self-contained, reproducible code? HTH, Jorge On Fri, Nov 5, 2010 at 3:54 PM, Wade Wall <> wrote:> Hi all, > > I have a dataframe (df1) that I am trying to select values from to a second > dataframe that at the current time is only for the selected items from df1 > (df2). The values that I am trying to save from df1 are factors with > alphanumeric names > > df1 looks like this: > > 'data.frame': 3014 obs. of 13 variables: > $ Num : int 1 1 1 2 2 2 3 3 3 4 ... > $ Tag_Num : int 1195 1195 1195 1162 1162 1162 1106 1106 1106 1173 ... > $ Site : Factor w/ 25 levels "PYBR002A","PYBR003B",..: 1 1 1 1 1 1 > 1 > 1 1 1 ... > $ Site_IndNum : Factor w/ 1044 levels "PYBR002A_001",..: 1 1 1 2 2 2 3 3 3 > 4 ... > .... ... > $ Area : num 463.3 29.5 101.8 152.9 34.6 ... > > However, whenever I try to assign values, like this > > df2[j,1]<-df2$Site[i] > > the values are changed from alphanumeric (e.g. PYBR003A) to numerals (e.g. > 1). > > Does anyone know why this is happening and how I can assign the actual > values from df1 to df2? > > Thanks in advance, > > Wade > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
Your example looks like you are assigning back to the first column of df2 (Num). Is this what you are really doing in your code? You need to follow the posting guide: PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. On Fri, Nov 5, 2010 at 3:54 PM, Wade Wall <wade.wall at gmail.com> wrote:> Hi all, > > I have a dataframe (df1) that I am trying to select values from to a second > dataframe that at the current time is only for the selected items from df1 > (df2). ?The values that I am trying to save from df1 are factors with > alphanumeric names > > df1 looks like this: > > 'data.frame': ? 3014 obs. of ?13 variables: > ?$ Num ? ? ? ? : int ?1 1 1 2 2 2 3 3 3 4 ... > ?$ Tag_Num ? ? : int ?1195 1195 1195 1162 1162 1162 1106 1106 1106 1173 ... > ?$ Site ? ? ? ?: Factor w/ 25 levels "PYBR002A","PYBR003B",..: 1 1 1 1 1 1 1 > 1 1 1 ... > ?$ Site_IndNum : Factor w/ 1044 levels "PYBR002A_001",..: 1 1 1 2 2 2 3 3 3 > 4 ... > ?.... ... > ?$ Area ? ? ? ?: num ?463.3 29.5 101.8 152.9 34.6 ... > > However, whenever I try to assign values, like this > > df2[j,1]<-df2$Site[i] > > the values are changed from alphanumeric (e.g. PYBR003A) to numerals (e.g. > 1). > > Does anyone know why this is happening and how I can assign the actual > values from df1 to df2? > > Thanks in advance, > > Wade > > ? ? ? ?[[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 Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Perhaps this will help:> test1 <- test2 <- data.frame(col1=factor(c(1:3), labels=c("a", "b", "c"))) > test3 <- data.frame(col1 = 1:3)Now:> test2[2,1] <- test1$col1[1] > test2$col1[1] a a c Levels: a b c vs> test3[2,1] <- test1$col1[1] > test3$col1[1] 1 1 3 Because test3's first column, col1, is a vector of numeric, and each element of a vector must have the same data type (numeric, factor, etc), it will coerce the data coming in to have the same data type (if it can). In this case, the data type is numeric. Had it been a character coming in, because it can't coerce a character to a numeric, it would have made the entire vector a vector of characters:> test3[2,1] <- 'b' > test3$col1[1] "1" "b" "3" Hope that demonstrates what's probably going on, Jeff. On Fri, Nov 5, 2010 at 3:54 PM, Wade Wall <wade.wall at gmail.com> wrote:> Hi all, > > I have a dataframe (df1) that I am trying to select values from to a second > dataframe that at the current time is only for the selected items from df1 > (df2). ?The values that I am trying to save from df1 are factors with > alphanumeric names > > df1 looks like this: > > 'data.frame': ? 3014 obs. of ?13 variables: > ?$ Num ? ? ? ? : int ?1 1 1 2 2 2 3 3 3 4 ... > ?$ Tag_Num ? ? : int ?1195 1195 1195 1162 1162 1162 1106 1106 1106 1173 ... > ?$ Site ? ? ? ?: Factor w/ 25 levels "PYBR002A","PYBR003B",..: 1 1 1 1 1 1 1 > 1 1 1 ... > ?$ Site_IndNum : Factor w/ 1044 levels "PYBR002A_001",..: 1 1 1 2 2 2 3 3 3 > 4 ... > ?.... ... > ?$ Area ? ? ? ?: num ?463.3 29.5 101.8 152.9 34.6 ... > > However, whenever I try to assign values, like this > > df2[j,1]<-df2$Site[i] > > the values are changed from alphanumeric (e.g. PYBR003A) to numerals (e.g. > 1). > > Does anyone know why this is happening and how I can assign the actual > values from df1 to df2? > > Thanks in advance, > > Wade > > ? ? ? ?[[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. >
Reasonably Related Threads
- [PATCH-v2 0/3] target/vhost-scsi: Add per-cpu ida tag pre-allocation for v3.12
- [PATCH-v2 0/3] target/vhost-scsi: Add per-cpu ida tag pre-allocation for v3.12
- [PATCH-v3 0/4] target/vhost-scsi: Add per-cpu ida tag pre-allocation for v3.12
- [PATCH-v3 0/4] target/vhost-scsi: Add per-cpu ida tag pre-allocation for v3.12
- [PATCH 0/3] Use sbitmap instead of percpu_ida