简体   繁体   中英

Add a new column in data.frame based on other columns

I want to add a new column called "FILE" in my data frame, the value has to be the word "LAT"+ number of the first column + the world "LON" + the number of the second column. Here is my data frame:

> fileN<-read.csv("files2.csv")
> fileN
         LAT    LON 
1        1.25 -78.75
2        0.75 -79.75
3        0.75 -79.25

And, I want to add the next column:

          LAT    LON  FILE
1        1.25 -78.75  LAT1.25LON-78.75
2        0.75 -79.75  LAT0.75LON-79.75
3        0.75 -79.25  LAT0.75LON-79.25

Please, could you help me

You can paste all elements.

library(dplyr)

fileN %>%
 mutate(FILE = paste0("LAT", LAT, "LON", LON)  
   library(dplyr)
   fileN <- fileN %>% mutate(FILE = paste0("LAT",LAT, "LON", LON)

Should work!

fileN$FILE=paste0("LAT",fileN$LAT,"LON",fileN$LON)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM