I created two data frame. One is blank to insert value from the other data frame. However, it doesn't insert the right value, it instead inserted some random numbers. Please advise.
this is my code:
name <- c(54,000,00)
age <- c(5, 3, 1)
weight <- c(120, 23, 245)
address <- c("kg", "kgi", "gg")
df1 <- data frame of name, age, weight, and address.
df2 <- # created 4 columns and 0 rows.
colnames(df2)[1] <- "Name"
colnames(df2)[2] <- "age"
colnames(df2)[3] <- "weight"
colnames(df2)[4] <- "address"
for (i in 1: ncol(df2)){
hd1 = colnames(df2)[i]
j <- 1
while (j <= ncol(df1)){
hd2 = colnames(df1)[j]
if(hd1 == hd2){
x <- 1
while(x <= nrow(df1)) {
df2[x,hd1] <- df1[x,colnames(df1)[j]]
x = x + 1
}
}
j = j + 1
}
}
the result of df2 is:
Where does the 2, 3, 1 come from in the address column.
Name age weight address
NA 5 120 2
NA 3 23 3
NA 1 245 1