简体   繁体   English

如何将一个数据框的行添加到另一个的列中

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

I have 2 dataframes. 我有2个数据框。 One is an empty dataframe called euro_adj: 一个是称为euro_adj的空数据框:

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

And another one called to_add that I want to "append" into this empty dataframe. 另外一个叫做to_add的我想“附加”到这个空的数据框中。 The first row is EUR and the second row is USD. 第一行是EUR,第二行是USD。

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

Eventually I want this final df: 最终我想要这个最终的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  

I tried this 我试过了

  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)

but it didn't work since the empty data frame only has 1 row right now. 但是它没有用,因为空数据框现在只有1行。 I figure I have to use rbind or something but I don't know how. 我认为我必须使用rbind之类的东西,但我不知道如何使用。

If I understood it right, you could use function 'gather()' of tidyr and so use within() function of base . 如果我的理解是正确的,你可以使用函数“聚集()”的tidyr等使用within()的功能base

Check if it solve your problem: 检查它是否解决了您的问题:

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

Wich gives: 威奇给出:

   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.

相关问题 R:如何将一个 dataframe 的列添加到另一个? - R: How do I add columns from one dataframe to another? 在R中,当两个数据帧中的某些值相等时,如何将数据帧中的某些特定列添加到另一个数据帧? - In R, how can I add some specific columns from a dataframe to another dataframe when some values are equal in both dataframes? 如何删除 dataframe 的所有列中具有相同元素的行? - How can I remove rows with same elements in all columns of a dataframe? 在 R 中,当复制到的 df 有 2 个附加列时,如何将行从一个数据帧复制到另一个数据帧? - In R, how can I copy rows from one dataframe to another when the df being copied to has 2 additional columns? 如何仅将列添加到数据框中的特定行 - How to add columns just to specific rows in dataframe 如何通过 for 循环在数据框中添加更多列 - How can i add more columns in dataframe by for loop 如何检查一个数据框中的两列是否都匹配另一个数据框中的两列? - How can I check that two columns in one dataframe both match two columns in another dataframe? 如何根据另一个数据帧的 2 列(开始和结束)中指定的范围标记一个数据帧的行? - How can I label rows of one dataframe according to a range specified in 2 columnns (start and end) of another dataframe? 如何检查r中数据框中的一列值是否对应于数据框中每一行的另一列? - How can I check if a value of a column in a dataframe in r corresponds to a single occurence of another columns for each row of the dataframe? 如何在R中向数据框添加另一列,以显示其他两个数据框的列之间的差异? - How do I add another column to a dataframe in R that shows the difference between the columns of two other dataframes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM