将数据从一个数据帧转换到另一个。。。。。基本的R程序设计。
在每次迭代中,我都会在for循环中得到以下样式的表将数据从一个数据帧转换到另一个。。。。。基本的R程序设计。,r,dataframe,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
> 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>
>表格
状态描述
日期:1945年11月19日星期一
2型:柯蒂斯R5C-1突击队(C-46)
3号接线员:美国海军陆战队
4注册:39592
5 C/n/msn:87
6第一次飞行:1944年
7名乘员:死亡人数:0/乘员:
8名乘客:死亡人数:0/乘客:
9总计:死亡人数:0/人:
10飞机损坏:损坏无法修复
11地点:Hishi no Shima(日本)
12阶段:未知(UNK)
13性质:军事
14起飞机场:?
15目的地机场:?
16叙事:武力登陆。
17可能原因:
在每次迭代中,我希望将其附加到以下数据帧:
>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)
>个人身份
[1] 日期时间类型操作员注册
[6] C_n_msn首飞机组乘客总数
[11] 飞机\u损坏位置阶段性质出发\u机场
[16] 目的地\机场叙述可能\原因发动机航班号
[21]从自行车租赁的总机身时间飞机运行时间
[26]碰撞现场海拔地面伤亡人员由代表
(或长度为0的行名称)
nrow(表$Status)不断更改每个记录,并相应地更改对它的描述。它的所有可能值都包含在colnames(个人_状态)中
有人可以指导我如何以正确的方式为每次迭代更新单个状态data.frame。这里我创建了一个包含四列的最小示例:
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))
}
status\u codes1这个怎么样:
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)
table$Status请您提供一个指向您的csv或tsv格式的公共数据文件的链接,以便我们可以导入它?虽然不是100%,但鉴于我的要求不明确,太不明确,无法放在任何地方,我感谢您的努力。在实施了你的建议后,我得到了解决办法。
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)