Cristian Cristian -3 years ago 135
R Question

Using data from csv file as vector of characters in R

I am new to R and am trying to use the SocialMediaMineR package to resolve shortened URLs that I have stored in a csv dataset. This is done with the command get_url, which requires a vector of characters as the URLs to be resolved.

Let us say I want to resolve three URLS. If I manually create a vector with those three characters and launch the command, it works:

url<- c("", "", "")
get_url(url, return.df = T)
... originalURL

However, I would like the list of URLs to be taken directly from my csv file. I tried the following code with a csv file that contains the exact same three URLs as before:

url_list = read.csv("~/Desktop/url2.csv", header = F)

But, I keep getting the following error:

Error : length(url) == 1 is not TRUE

For clarity, here is the structure of the data as read from the csv file:

> url_list<-read.csv("~/Desktop/url2.csv", header = F)
> dput(url_list)
structure(list(V1 = structure(1:3, .Label = c("",
"", ""), class = "factor")), .Names = "V1", class = "data.frame", row.names = c(NA,

Why is this happening and how can I fix it? Any help would be hugely appreciated!

Answer Source

First of all you haven't read the data using argument stringsAsFactors = FALSE, so you have a column of objects of class factor. You must therefore do

url_list$V1 <- as.character(url_list$V1)

After this the following seems to work.


Note: The proper way of reading in the data would have been, like I've said above,

url_list = read.csv("~/Desktop/url2.csv", header = F, stringsAsFactors = FALSE)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download