我想重新排列数据框中的数据,该数据框最初采用以下格式
我使用“rvest”包
提取了这个library(rvest)
library(plyr)
library(dplyr)
projects<-NULL
thepage = read_html("https://www.99acres.com/search/project/buy/residential/pune?search_type=QS&search_location=SH&lstAcn=NPSEARCH&lstAcnId=9753976212484323&src=CLUSTER&preference=S&city=19&res_com=R&selected_tab=3&isvoicesearch=N&keyword=pune&strEntityMap=IiI%3D&refine_results=Y&Refine_Localities=Refine%20Localities&action=%2Fdo%2Fquicksearch%2Fsearch&searchform=1&price_min=null&price_max=null")
table = data.frame(html_table(x = thepage, fill = TRUE))
table = as.data.frame(t(table))
ResidentialProjects<-rbind(projects,setNames(table, names(table)))
我想要一行中有关1个项目(房地产项目名称)的所有细节。 我尝试为它编写代码
newdf<-data.frame(matrix(ncol = 10),stringsAsFactors = FALSE)
df=ResidentialProjects
projectName=""
count<-0
for(n in 1:nrow(df)){
if(df[n,]$V1!=projectName){
count = count+1
projectName=df[n,]$V1
newdf[count,c(1,2,3,4)]=df[n,c(1,2,3,4)]
newdf[count,c(5,6,7)]=df[n+1,c(2,3,4)]
newdf[count,c(8,9,10)]=df[n+2,c(2,3,4)]
}else{
print(n)
next
}
}
但它给了我一个数字表,如
有什么问题?或者更好的选择??