0

I have a variable named likes in R (retrieved from Rfacebook package). This variable contains all my own likes as characters. I've transformed this variable into a factor variable so that I can analyse it. Though, for the sake of my project, I need to get all my 3 different likes in the header as separate variables in a dataframe, with only myself as a row.

Context: If more people than me will be added to the dataframe, then these likes of other people should be added as variables as well and the cells are filled in with either the name of the page if they like the page and an NA if they haven't liked the page.

Help would be much appreciated!

Here is the problem: When I retrieve data from facebook the likes column returns an NA because data it contains exists of multiple characters (the three likes) as shown here.

Then I run the following code:

Retrieve own user information

me <- getUsers("1131454363567865",token=fb_oauth, private_info = TRUE)
my_likes <- getLikes(user="me", token=fb_oauth)
likes <- as.factor(my_likes$names)
dflikes <- as.data.frame(likes)

Then I end up with a data.frame with the 3 likes listed in one column, while I actually want that these 3 different factor levels are transformed to 3 new variables. So lined up next to the other variables as name, gender, etc as shown here.

Here is what the entire code looks like:

 # Set up APP connection
install.packages("devtools")
library(devtools)

install_github("Rfacebook", "pablobarbera", subdir="Rfacebook")

require("Rfacebook")
fb_oauth <- fbOAuth(app_id="**", app_secret="**", extended_permissions = TRUE, legacy_permissions = TRUE)
save(fb_oauth, file="fb_oauth")
load("fb_oauth")

# Retrieve own user information
me <- getUsers("1131454363567865",token=fb_oauth, private_info = TRUE)
my_likes <- getLikes(user="me", token=fb_oauth)

likes <- as.factor(my_likes$names)
dflikes <- as.data.frame(likes)
nicola
  • 24,005
  • 3
  • 35
  • 56
Floris
  • 11
  • 7
  • 2
    The code you provided is not reproducible and I guess it is also irrelevant to the question. Show what you have at the moment and what is your desired output. As a side note, doesn't seem wise to add more columns as other users are added. The "long" format might be more useful. – nicola Sep 08 '16 at 12:00
  • 3
    Welcome to SO. Please have a read at [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and of course how to make [a reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). In addition, please include the expected output. – Sotos Sep 08 '16 at 12:01
  • @Sotos I've reframed my question for the sake of clarity. Hopefully this explanations would enable you guys to help me out :) I've two pictures included. – Floris Sep 08 '16 at 12:33
  • @nicola, not sure if you would get notified about my update anyways – Floris Sep 08 '16 at 12:34
  • 1
    Again, you are not providing a reproducible example. Did you read the link @Sotos posted? I reiterate my suggestion, since it wasn't clear enough. It seems to me that you have a `data.frame` and you want to reshape it. How can be useful the code you posted, in which said data.frame is generated, if the code is not reproducible? Post the output of `dput(dflikes)` and build "by hand" your desired output, so anyone can understand. – nicola Sep 08 '16 at 12:55
  • @nicola Thanks for your patience. Now it is exactly like you suggested including a reproducible code. It contains my app id and app secret... not sure if this could be harmful once made public... Would still be very much appreciated if you could help me out :) – Floris Sep 08 '16 at 13:20
  • Do not share your personal data! I didn't want you to do this. Just obtain your final result and *then* post the output of `dput(dflikes)`. – nicola Sep 08 '16 at 13:28
  • Also, see this http://stackoverflow.com/questions/6893367/is-it-safe-to-share-facebook-api-id-and-secret-key on the dangers to share your credentials. I edited your post and flagged your question hoping that a moderator could remove any reference on it. I suggest you to obtain other credentials. – nicola Sep 08 '16 at 13:32
  • @ nicola, thanks for your edit, think it will be fine. I think I found out what I meant by myself haha – Floris Sep 08 '16 at 13:47
  • me$Nienchli <- my_likes$names[1] me$Erasmus_Center_of_Entrepreneurship <- my_likes$names[2] me$YEStoselflove <- my_likes$names[3] – Floris Sep 08 '16 at 13:48
  • I did it this way as shown above... Hoped there was a way to kinda automize it.. or in case I add more users to my data.frame with different or similar likes. So any advices regarding this matter would be nice, otherwise this works as well. – Floris Sep 08 '16 at 13:49

0 Answers0