0

Have a dataframe (frame 1) with a column (fact) that is composed of factors I have created a new data frame(frame2) as follow:

Matching_list <- setNames(data.frame(matrix(ncol = 3, nrow = nrow(HH))), c("ProIDS", "Pol_Centre", "Dist"))

Now when I select some elements from the variable, fact, in the first dataframe and affect them to the new dataframe, all these elements are turned into integers. So, I see integer numbers (numbers that I don't know where they came from) instead the original description in the first dataframe.

Could you please give me some tips, how can I perform the affectation without loosing information (I mean, keep my factors as they are in the first data frame)

Sotos
  • 51,121
  • 6
  • 32
  • 66
  • 1
    Welcome to StackOverflow! Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). This will make it much easier for others to help you. – Sotos Mar 09 '20 at 10:33
  • A) A `factor` is internally stored as integer . In that sense you din't lose any information. B) A `matrix` can only hold data of one type. If you want mixed columns you should use `list`s (or `data.frame`, which are internally processed as list of lists) – dario Mar 09 '20 at 10:48
  • Also: If you add a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). you could make it easier for others to find and test a answer to your question. That way you can help others to help you! – dario Mar 09 '20 at 10:48
  • Thank you guys! I want to share bellow a sample of my code – TAMEZA Bertrand Borel Mar 09 '20 at 11:07
  • for (i in c(1: nrow(Gps_Households))){ Matching_list[i,1] = Gps_Households[which(Matrix_of_distance == min(Matrix_of_distance[i,]), arr.ind = T)[1],1] Matching_list[i,2] = Gps_poll_cent_west_urban[which(Matrix_of_distance == min(Matrix_of_distance[i,]), arr.ind = T)[2],1] Matching_list[i,3] = min(Matrix_of_distance[i,]) Column[i] = Gps_poll_cent_west_urban[which(Matrix_of_distance == min(Matrix_of_distance[i,]), arr.ind = T)[2],1] } – TAMEZA Bertrand Borel Mar 09 '20 at 11:09
  • But I think the problem is related to the fact that I am using a matrix, I should use a list instead. How can I use a matrix to fill up a list? – TAMEZA Bertrand Borel Mar 09 '20 at 11:15
  • What @dario had in mind (and others too) as wanting was not that you share code but that you share reproducible data! – Chris Ruehlemann Mar 09 '20 at 11:18
  • One more thing: when you say 'affect' and affectation' do oyu really mean 'add' or 'assign' and 'addition' or 'assigment'? – Chris Ruehlemann Mar 09 '20 at 11:20

1 Answers1

0

Here's a completely tentative solution to your problem (as you've not provided any reproducible data).

Assuming you have this kind of data:

df1 <- data.frame(fact = LETTERS[1:10])

if you look at the variable fact's structure, you will see it's a factor:

str(df1)
'data.frame':   10 obs. of  1 variable:
 $ fact: Factor w/ 10 levels "A","B","C","D",..: 1 2 3 4 5 6 7 8 9 10

Now further assuming that, as you say, you have another dataframe, like this:

set.seed(100)
df2 <- data.frame(varX = rnorm(10))

and that it is to this dataframe that you want to assign some factor level from fact depending on some condition, and want to get in the output not an integer but the actual factor level, this could be done using levels:

df2$fact <- ifelse(df2$varX < 0, levels(df1$fact)[1], NA) # if varX < 0 "A" else NA
df2
          varX fact
1  -0.50219235    A
2   0.13153117 <NA>
3  -0.07891709    A
4   0.88678481 <NA>
5   0.11697127 <NA>
6   0.31863009 <NA>
7  -0.58179068    A
8   0.71453271 <NA>
9  -0.82525943    A
10 -0.35986213    A

But note that if you now check the structure of df2, R has converted df2$factinto a character variable:

str(df2)
'data.frame':   10 obs. of  2 variables:
 $ varX: num  -0.5022 0.1315 -0.0789 0.8868 0.117 ...
 $ fact: chr  "A" NA "A" NA ...
Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34