Sijo K
Sijo K

Reputation: 103

R: How to refer the value of a dataframe in for loop in R

I have a data frmae like this

Tweets
1                                                                                 Thank you @DiscoveryIN. very inspirational to watch #Jaiho
2               @DiscoveryIN @arrahman Your music has always been inspirational sir.  Your life is doubly inspirational. Thanks for sharing.
3                        @oyorooms @OYO4U Thanks for listening to my concerns. Hope things will turn better in future. Booking id - DYER2375
4 @oyorooms Next time when i will book thru @makemytrip. Or worst, I will sleep in my car  than booking thru @oyorooms . Booking id DYER2375

I want to read the above data frame in R inside for loop

for(i in 1:nrow(tweets)){
  TEXT = URLencode('tweets[i,]')  >>>>>>>>>
  print(TEXT)
}

I want to refer the value of first record inside the for loop. However printing this gives a result like the below instead of the actual value.

> TEXT
[1] "tweets[i,]"

How to get the actual value?

Should be something like this

> print(TEXT)
[1] "@DiscoveryIN%20Thank%20you%20@DiscoveryIN.%20very%20inspirational%20to%20watch%20#Jaiho"

How to achieve this?

Upvotes: 0

Views: 399

Answers (1)

Jake Kaupp
Jake Kaupp

Reputation: 8072

You need to have the value printed as a character string for URLencode.

You could use sprintf

URLencode(sprintf("%s",tweets[i,1]))

or you could use paste

URLencode(paste(tweets[i,1]))

Upvotes: 2

Related Questions