簡體   English   中英

將列表轉換為 R 中數據框列中的字符串

[英]convert list to string in dataframe column in R

我有一個數據框,其中一列包含列表數據,想將其轉換為字符串,當我對數據框進行子集化時,我得到的值低於值--> df[2,4]

[1] "marianna g: hello dr ydeen how can i help you"  
                                                                                                                                                                                                                                    
[2] "marianna g: yes you can view that in your secure hr page under benefits summary first youll have to enter the last four of your ssn on the right hand side youll be directed to your secure hr page and on the left hand side there will be a list of links select benefits summary"

[3] "marianna g: you can also view it on each pay stub biweekly"  
                                                                                                                                                                                                                       
[4] "marianna g: youll have to select the quotprintviewquot option on the paycheck and the accruals are listed under your earnings"  
                                                                                                                                                    
[5] "marianna g: great yes thats correct"   
                                                                                                                                                                                                                                             
[6] "marianna g: its your accruals as of the last pay period"    
                                                                                                                                                                                                                        
[7] "marianna g: so currently its accurate up until"    
                                                                                                                                                                                                                                 
[8] "marianna g: my pleasure anything else i can help you with"    
                                                                                                                                                                                                                      
[9] "marianna g: have a great day"

我想將它轉換為字符串,所以當我執行 df[2,4] 時,我應該得到以下輸出:

"marianna g: hello dr ydeen how can i help you, marianna g: yes you can view that in your secure hr page under benefits summary first youll have to enter the last four of your ssn on the right hand side youll be directed to your secure hr page and on the left hand side there will be a list of links select benefits summary, marianna g: you can also view it on each pay stub biweekly, marianna g: youll have to select the quotprintviewquot option on the paycheck and the accruals are listed under your earnings, marianna g: great yes thats correct, marianna g: its your accruals as of the last pay period, marianna g: so currently its accurate up until, marianna g: my pleasure anything else i can help you with, marianna g: have a great day"

似乎 df[2,4] 是一個向量。 要使其成為字符串,只需使用paste函數。 作為參數,您需要指定collapse=" "將所有內容粘貼在一起,中間有一個空格:

paste(df[2,4],collapse=" ")

如果你想讓它們以逗號分隔在一個字符串中,只需使用

paste(df[2,4],collapse=", ")

您應該能夠將該列傳遞給 paste() 函數,然后將其重新分配給該列:

df[2,4] <- paste(df[2,4])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM