阅读第3和第4列并附加到第2列

时间:2015-08-03 10:57:44

标签: r dataframe

我创建了一个attraction_stats_score数据框,如下所示:

> attraction_stats_score[(1:2)]
                    TOURISTATTRACTIONSITES1 TOTAL
1                              Orchard Road    41
2                                 Chinatown    23
3                              Little India    19
4                              Merlion Park    19
5  Singapore River (Clarke Quay/ Boat Quay)    17
6                             Raffles Hotel    11
7                 Singapore Botanic Gardens    10
8             Esplanade Theatres by the Bay     5
9               Integrated Resort (MBS/RWS)    33
10                                  Sentosa    17
11                       Gardens by the Bay    12
12       Singapore Flyer/ Flight Experience     8
13                             Night Safari     8
14                            Singapore Zoo     7
15                  National Orchid Gardens     5
16                         Jurong Bird Park     3

现在,我在第3和第4列中有相同景点的国家明智数据。

如何将第3列与现有的第1列和第2列合并?

> attraction_stats_score[c(1,3)]
                    TOURISTATTRACTIONSITES1 INDONESIA
1                              Orchard Road        49
2                                 Chinatown        13
3                              Little India        13
4                              Merlion Park        12
5  Singapore River (Clarke Quay/ Boat Quay)         4
6                             Raffles Hotel         2
7                 Singapore Botanic Gardens         2
8             Esplanade Theatres by the Bay         4
9               Integrated Resort (MBS/RWS)        24
10                                  Sentosa         9
11                       Gardens by the Bay         7
12       Singapore Flyer/ Flight Experience         4
13                             Night Safari         1
14                            Singapore Zoo         2
15                  National Orchid Gardens         1
16                         Jurong Bird Park         1

任何指导都会非常有用。

编辑:

最后我需要:

TOURISTATTRACTIONSITES1 TOTAL
    1                              Orchard Road    41
    2                                 Chinatown    23
    3                              Little India    19
    4                              Merlion Park    19
    5  Singapore River (Clarke Quay/ Boat Quay)    17
    6                             Raffles Hotel    11
    7                 Singapore Botanic Gardens    10
    8             Esplanade Theatres by the Bay     5
    9               Integrated Resort (MBS/RWS)    33
    10                                  Sentosa    17
    11                       Gardens by the Bay    12
    12       Singapore Flyer/ Flight Experience     8
    13                             Night Safari     8
    14                            Singapore Zoo     7
    15                  National Orchid Gardens     5
    16                         Jurong Bird Park     3
                        TOURISTATTRACTIONSITES1 INDONESIA
    1                              Orchard Road        49
    2                                 Chinatown        13
    3                              Little India        13
    4                              Merlion Park        12
    5  Singapore River (Clarke Quay/ Boat Quay)         4
    6                             Raffles Hotel         2
    7                 Singapore Botanic Gardens         2
    8             Esplanade Theatres by the Bay         4
    9               Integrated Resort (MBS/RWS)        24
    10                                  Sentosa         9
    11                       Gardens by the Bay         7
    12       Singapore Flyer/ Flight Experience         4
    13                             Night Safari         1
    14                            Singapore Zoo         2
    15                  National Orchid Gardens         1
    16                         Jurong Bird Park         1

2 个答案:

答案 0 :(得分:1)

感谢朋友们的指导,

我接受了答案。由于列名不匹配,rbind发出错误。为了保留国家/地区的标签,我添加了一个国家/地区列。 代码使用循环,因为有24列要合并 它基于Dimitris代码片段:

#Finds the name of the ith column
newcol <- names(attraction_stats_score)[2]
temp1 <- attraction_stats_score[,c(1,2)]
temp1$Country <- newcol
names(temp1) < c("TOURISTATTRACTIONSITES1", "TOTAL", "Country")
attractionname <- temp1

for (i in 3:24)
{

  newcol <- names(attraction_stats_score)[i]
  temp2 <- attraction_stats_score[,c(1,i)]
  temp2$Country <- newcol
  names(temp2) <- c("TOURISTATTRACTIONSITES1", "TOTAL", "Country")
  attractionname <- rbind(attractionname,temp2)

}

# Writing the scoring data to a file
write.csv(attractionname, file = 'C:\\Datasets\\attraction_names.csv')

答案 1 :(得分:0)

执行以下操作

temp1 <- attraction_stats_score[, c(1, 3)]
names(temp1) <- c("TOURISTATTRACTIONSITES1", "TOTAL")

temp2 <- attraction_stats_score[, c(1, 3)]
names(temp2) <- c("TOURISTATTRACTIONSITES1", "TOTAL")

df <- rbind(attraction_stats_score[, c(1, 2)], temp1, temp2)

<强> 更新

根据您的评论,您可以执行以下操作,创建另一个包含国家/地区名称的列。

然后代码是:

temp <- attraction_stats_score[, c(1, 2)]
temp$Country <- "All"

temp1 <- attraction_stats_score[, c(1, 3)]
names(temp1) <- c("TOURISTATTRACTIONSITES1", "TOTAL")
temp1$Country <- "INDONESIA"

temp2 <- attraction_stats_score[, c(1, 3)]
names(temp2) <- c("TOURISTATTRACTIONSITES1", "TOTAL")
temp2$Country <- "AUSTRALIA"

df <- rbind(temp, temp1, temp2)