简体   繁体   中英

Extracting decimal values from a data frame in R

I have a GBIF data frame of the species distribution. Some of the occurrences are not in the long and lat columns. How can I extract the decimal values for each row (each species sample)? "$"

Assuming that you want to extract the decimal values of lat / long coordinates in decimal format, you could use something like this:

#Some sample data
df<-data.frame (Sp = paste0("Sp",seq(1,10,1)),
            lat = seq(1,9,0.88),
            long = seq (1,17,1.66))
#Get the values before (lat1 and long1) and after (lat2 and long2) the decimal point
df$lat1 <- floor(df$lat)
df$lat2 <- df$lat %% floor(df$lat)
df$long1 <- floor(df$long)
df$long2 <- df$long %% floor(df$long)

#The result looks like this:

#     Sp  lat  long lat1 lat2 long1 long2
#1   Sp1 1.00  1.00    1 0.00     1  0.00
#2   Sp2 1.88  2.66    1 0.88     2  0.66
#3   Sp3 2.76  4.32    2 0.76     4  0.32
#4   Sp4 3.64  5.98    3 0.64     5  0.98
#5   Sp5 4.52  7.64    4 0.52     7  0.64
#6   Sp6 5.40  9.30    5 0.40     9  0.30
#7   Sp7 6.28 10.96    6 0.28    10  0.96
#8   Sp8 7.16 12.62    7 0.16    12  0.62
#9   Sp9 8.04 14.28    8 0.04    14  0.28
#10 Sp10 8.92 15.94    8 0.92    15  0.94

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