簡體   English   中英

如何將一個數據框的行添加到另一個的列中

[英]How can I add rows of a dataframe into columns of another

我有2個數據框。 一個是稱為euro_adj的空數據框:

 flow country year frequency currency percentage notes
1   NA      NA   NA        NA       NA         NA    NA

另外一個叫做to_add的我想“附加”到這個空的數據框中。 第一行是EUR,第二行是USD。

1999 2000 2001 2002 
 NA   NA 89.08  NA
 NA   NA  60.2  NA

最終我想要這個最終的df:

flow   country year frequency currency percentage notes
Export Austria 1999   Annual     EUR       NA         NA   
Export Austria 2000   Annual     EUR       NA         NA 
Export Austria 2001   Annual     EUR       89.08      NA 
Export Austria 2002   Annual     EUR       NA         NA 
Export Austria 1999   Annual     USD       NA         NA 
Export Austria 2000   Annual     USD       NA         NA 
Export Austria 2001   Annual     USD      60.2        NA
Export Austria 2002   Annual     USD       NA         NA  

我試過了

  to_add_transpose = as.data.frame(t(to_add))
  colnames(to_add_transpose) = c("EUR", "USD")
  euro_adj$country = rep("Austria", ncol(to_add)*2)
  euro_adj&flow = rep("Exports", ncol(to_add)*2)
  euro_adj$year = rep(1999:2012, 2)
  euro_adj$frequency = rep("Annual", ncol(to_add)*2)
  euro_adj$percentage = c(to_add_trasnpose$EUR, to_add_transpose$USD)

但是它沒有用,因為空數據框現在只有1行。 我認為我必須使用rbind之類的東西,但我不知道如何使用。

如果我的理解是正確的,你可以使用函數“聚集()”的tidyr等使用within()的功能base

檢查它是否解決了您的問題:

library(dplyr)
library(tidyr)



# Yours data
to_add = data.frame(a=c(NA,NA),  
                    b=c(NA,NA), 
                    c=c(89.08,60.2),
                    d=c(NA,NA)
) 
colnames(to_add) = c("1999","2000","2001","2002")

# Creating column of "coin"
to_add["coin"] = c("EUR","USD")


# Just numbers of rows and columns
nr = nrow(to_add)
nc = ncol(to_add)-1

# Transforming data, like "transpose"
to_add = gather(data = to_add,"year","value",1:4)


# build another data frame
euro_adj = data.frame(flow=rep("Exports", nc*nr),
                      country=rep("Austria", nc*nr),
                      year=rep(1999:2002, nr),
                      frequency=rep("Annual", nc*nr),
                      # Set curency by order
                      currency= to_add[order(to_add$coin),"coin"],
                      percentage=NA[1:nc*nr],
                      notes=NA[1:nc*nr]
                      )

# set values

set = to_add[!is.na(to_add$value),c("coin","year","value")]

euro_adj = euro_adj %>% 
            within(percentage[year %in% set$year & currency %in% set$coin] <- set$value)

# Print data frame 
euro_adj

威奇給出:

   flow country year frequency currency percentage notes
Exports Austria 1999    Annual      EUR         NA    NA
Exports Austria 2000    Annual      EUR         NA    NA
Exports Austria 2001    Annual      EUR      89.08    NA
Exports Austria 2002    Annual      EUR         NA    NA
Exports Austria 1999    Annual      USD         NA    NA
Exports Austria 2000    Annual      USD         NA    NA
Exports Austria 2001    Annual      USD      60.20    NA
Exports Austria 2002    Annual      USD         NA    NA

暫無
暫無

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

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