【问题标题】:Transposing data from one dataframe to another..... Basic R Programming.将数据从一个数据帧转移到另一个......基本的 R 编程。
【发布时间】:2017-12-03 15:10:26
【问题描述】:

在每次迭代时,我都会在 for 循环中得到以下样式的表格。

> table
             Status                   Description
1                 Date:       Monday 19 November 1945
2                 Type: Curtiss R5C-1 Commando (C-46)
3             Operator:    United States Marine Corps
4         Registration:                         39592
5            C/n / msn:                            87
6         First flight:                          1944
7                 Crew:    Fatalities: 0 / Occupants:
8           Passengers:    Fatalities: 0 / Occupants:
9                Total:    Fatalities: 0 / Occupants:
10     Airplane damage:         Damaged beyond repair
11            Location:     Hishi-no-Shima (   Japan)
12               Phase:                 Unknown (UNK)
13              Nature:                      Military
14   Departure airport:                             ?
15 Destination airport:                             ?
16           Narrative:                 Force landed.
17      Probable Cause:                          <NA>

在每次迭代中,我希望将其附加到以下数据框:

>individual_status
[1] Date                 Time                 Type                 Operator             Registration        
[6] C_n_msn              First_flight         Crew                 Passengers           Total               
[11] Airplane_damage      Location             Phase                Nature               Departure_airport   
[16] Destination_airport  Narrative            Probable_Cause       Engines              Flightnumber        
[21] Total_airframe_hrs   Airplane_fate        Operating_for        Leased_from          Cycles              
[26] Crash_site_elevation Ground_casualties    Operated_by          On_behalf_of        
<0 rows> (or 0-length row.names)

nrow(table$Status) 每条记录都在不断变化,相应的描述也随之变化。它的所有可能值都包含在 colnames(individual_status)

有人可以指导我如何以正确的方式为每次迭代更新 individual_status data.frame。

【问题讨论】:

  • 请您提供一个指向您的 csv 或 tsv 格式数据的公共文件的链接,以便我们可以导入它?

标签: r dataframe


【解决方案1】:

这个怎么样:

table$Status <- gsub(":", "", table$Status)

reshapedTable <- data.frame(lapply(table$Description, function(x) 
t(data.frame(x))))
names(reshapedTable) <- table$Status

require(plyr)
rbind.fill(reshapedTable, individual_status)

【讨论】:

  • 虽然不是 100%,但考虑到我不清楚的要求,这些要求太蹩脚而无法放在任何地方,我感谢您的努力。实施您的建议后,我得到了解决方案。
【解决方案2】:

在这里,我创建了一个四列的最小示例:

status_codes1 <- c("Date", "Type", "Operator", "Registration")
status_codes2 <- paste(status_codes1, ":", sep = "") 

table1 <- data.frame(Status = status_codes2, Description = 1:4, stringsAsFactors = F)
table1

individual_status <- setNames(data.frame(matrix(ncol = 4, nrow = 0)), sample(status_codes1))

table2 <- table1[sample(1:4),]

append_to_is <- function()
{
    table2 <- table1[sample(1:4),]
    n_row <- nrow(individual_status)
    cols <- gsub(":", "", table2$Status)
    individual_status[n_row + 1, cols] <<- table2$Description
    return(list(table2, individual_status))
}

看到,带有“table2”的行:

table2 <- table1[sample(1:4),]

创建原始 table1 的副本,其中包含随机列。无论以何种顺序显示新表,首先正则表达式替换删除尾随冒号“:”,然后使用列名子集主 df 并附加到下一行。

该函数返回打乱的表和附加的个人状态。您可以根据自己的喜好重述该功能。

【讨论】:

    猜你喜欢
    • 2020-08-20
    • 1970-01-01
    • 1970-01-01
    • 2021-01-09
    • 2022-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多