I have searched a lot and I didn't find an answer on this. Let's say we have some data from a .csv file(lets call it xx.csv). Something like this
Number A B C ... Z
1 .. .. ..
.
.
.
4000 .. .. .. ... ...
You can put whatever you want in A, B, C,...Names, numbers, NAs etc. So, Whats the easiest way that I replace a whole column (lets say B) with another one external (I mean not one from the csv file)??
With assignment:
data$B <- whatever
# or
data[, "B"] <- whatever
# or
data[["B"]] <- whatever
First I set up an example people.csv
.
names <- c("Alice", "Bob", "Carol")
ages <- c(18,21,19)
eyecolor <- c("Blue", "Brown", "Brown")
df <- data.frame(names, ages, eyecolor)
write.csv(df, "people.csv")
Then I replace the age column by a height column:
height <- c(160, 180, 170)
df <- read.csv("people.csv")
df[["ages"]] <- height
colnames(df)[colnames(df) == "ages"] <- "height"
write.csv(df, "people.csv")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With