[英]Loop to clean up table where observations are stored as column
I have a table that stores observations in column x
and variable names in column y
the following way.我有一个表,按以下方式在
x
列中存储观察值,在y
列中存储变量名称。
I am trying to write an R
loop to create a matrix where each observation is a row and each variable is a column.我正在尝试编写一个
R
循环来创建一个矩阵,其中每个观察值是一行,每个变量是一列。
The problem is that not all observations have all the variables.问题是并非所有观察结果都包含所有变量。
Original data:原始数据:
x ![]() |
y![]() |
---|---|
Apple![]() |
Fruit![]() |
Austria![]() |
Origin![]() |
Summer![]() |
Season![]() |
Orange![]() |
Fruit![]() |
Spain![]() |
Origin![]() |
Pear![]() |
Fruit![]() |
Tomato![]() |
Fruit![]() |
Italy![]() |
Origin![]() |
Summer![]() |
Season![]() |
Desired output:所需的 output:
Fruit![]() |
Origin![]() |
Season![]() |
---|---|---|
Apple![]() |
Austria![]() |
Summer![]() |
Orange![]() |
Spain![]() |
|
Pear![]() |
||
Tomato![]() |
Italy![]() |
Summer![]() |
My thinking so far (pseudo R
code):到目前为止我的想法(伪
R
代码):
df_old <- data.frame( x = c( "Apple", "Austria", "Summer", "Orange", "Spain", "Pear", "Tomato", "Italy", "Summer" ),
y = c( "Fruit", "Origin", "Season", "Fruit", "Origin", "Fruit", "Fruit", "Origin", "Season" ) )
df_new <- data.frame( matrix( ncol = 3, nrow = 0 ) )
colnames( df_new ) <- c( "Fruit", "Origin", "Season")
for ( i in seq_along( df_old ) ) {
if ( y == "Fruit" ) {
# add new row
df_new$Fruit <- df_old$x
} else if ( y == "Origin" ) {
df_new$Origin <- df_old$x
} else ( y == "Season" ) {
df_new$Season <- df_old$x
}
}
Thank you for helping.谢谢你的帮忙。
Here is a solution based on the idea you were giving using a for-loop.这是一个基于您使用 for 循环给出的想法的解决方案。
df_old <- data.frame( x = c( "Apple", "Austria", "Summer", "Orange", "Spain", "Pear", "Tomato", "Italy", "Summer" ),
y = c( "Fruit", "Origin", "Season", "Fruit", "Origin", "Fruit", "Fruit", "Origin", "Season" ) ,stringsAsFactors=F)
df_new <- as.data.frame(matrix(NA, nrow=sum(df_old$y == "Fruit"), ncol=length(unique(df_old$y))))
names(df_new) <- c("Fruit", "Origin", "Season")
j <- 0
for (i in 1:(nrow(df_old))){
print(df_old$y[i])
if (df_old$y[i] == "Fruit") { j <- j + 1 ; df_new$Fruit[j] <- df_old$x[i]
print("new colum")
if ((df_old$y[i+1] == "Origin")){ df_new$Origin[j] <- df_old$x[i+1] }
print("add origin")
if ((df_old$y[i+1] == "Season") | (df_old$y[i+2] == "Season")){
df_new$Season[j] <- df_old$x[c(i+1,i+2)][df_old$y[c(i+1,i+2)] == "Season"]
print("add Season")
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.