R:如何在R中的for循环中引用数据帧的值

时间:2016-12-02 18:32:04

标签: r

我有像这样的数据

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

我想在R里面读取上面的数据帧for loop

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

我想在for循环中引用第一条记录的值。但是打印时会产生如下结果,而不是实际值。

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

如何获得实际价值?

应该是这样的

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

如何实现这一目标?

1 个答案:

答案 0 :(得分:2)

您需要将值打印为URLencode的字符串。

您可以使用sprintf

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

或者您可以使用paste

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