将数据从一个数据帧转换到另一个数据帧......基本R编程。

时间:2017-12-03 15:10:27

标签: r dataframe

我在每次迭代时都在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。

2 个答案:

答案 0 :(得分:1)

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

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子集化并附加到下一行。

该函数返回混洗表和附加的个别状态。您可以根据自己的喜好重新调整功能。

答案 1 :(得分: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)