奇数甚至在r

时间:2015-03-04 15:34:34

标签: r

    persnr     date   
411223-6213 2011-01-19 
420211-6911 2012-01-19 
420604-7716 2007-09-01 
430404-8558 2011-09-01 
431030-7030 2011-09-01
440127-0055 2012-09-01

如果第10位是奇数或偶数,我想为persnr创建一个新列。 根据{{​​1}}的第10位是奇数还是偶数,新列将是真还是假。 odd = true,even = false

我还想创建另一个列för'date',例如2011-09-01是秋天,而新列是fall = true 2012-01-19是春季,新栏目是spring = false。

这当然是基本的,但我是R中的新用户,可能不是正确的。

2 个答案:

答案 0 :(得分:6)

您可以尝试substr。不确定您是否也计算-字符。在那种情况下,

 v1 <- as.numeric(substr(df1$persnr,10,10))

或者在{@ 1}}中将10替换为@ nico的帖子

11

我希望将其作为逻辑列,但如果您需要将其更改为“奇数”,“甚至&#39;

 df1$newCol <- as.logical(v1%%2)

答案 1 :(得分:1)

# Generate the data
my.data <- data.frame(
  persnr=c("411223-6213", "420211-6911",
           "420604-7716", "430404-8558",
           "431030-7030", "440127-0055"),
  date = c("2011-01-19", "2012-01-19",
           "2007-09-01", "2011-09-01",
           "2011-09-01", "2012-09-01"))

# Get the 10th digit of persnr using substring, then check the reminder
# of its division by 2 to determine if it is odd or even
# Note that I get the 11th char as there is a - in the middle of the number
digit.10 <- substr(my.data$persnr, 11, 11)
my.data$evenOdd <- ifelse(as.integer(digit.10)%%2, "odd", "even")
my.data$evenOdd <- factor(my.data$evenOdd, levels=c("odd", "even"))

查找每个日期的季节:

# Get month and day, ignore year
month.day <- strftime(my.data$date, format="%m-%d")
# Now check which season we're in -- ASSUMING NORTHERN HEMISPHERE, change if needed
# Also note that the dates of solstices and equinoxes are variable so
# this is approximative...

# Set everyone to winter
my.data$season <- "Winter"

# Find indices for the other seasons
spring <- which(month.day >= "03-21" & month.day < "06-21")
summer <- which(month.day >= "06-21" & month.day < "09-21")
fall <- which(month.day >= "09-21" & month.day < "12-21")

my.data$season[spring] <- "Spring"
my.data$season[summer] <- "Summer"
my.data$season[fall] <- "Fall"

my.data$season <- factor(my.data$season, levels = 
                  c("Spring", "Summer", "Fall", "Winter"))