Domanda

I have a dataframe which contains a date column:

          date  t14  rh14
1   2013-05-01 14.8  56.5
2   2013-05-02 14.5  71.8
3   2013-05-03 17.5  40.3
4   2013-05-04 19.0  34.6
5   2013-05-05 21.4  45.3

in which the month of the date can be extracted (I used the following:)

date=as.Date(paste(df.date$year,df.date$month,df.date$day, sep="-"),format="%Y-%m-%d")

Now I want a new column in my dataframe, which is a factor f depending on the month (January -> first value, etc)

factor=c(0.22,0.22,0.22,0.29,0.29,0.28,0.26,0.25,0.23,0.22,0.22,0.22)

Been looking for a solution for a while and still don't really know where to start. Still pretty new to R and programming in general. Any help would be appreciated a lot. Thanks in advance!

È stato utile?

Soluzione 2

It sounds like you already have month stored as a separate variable in the df.date data frame. If so, you could create a factor from it like so:

Haude.input$monthF <- factor(df.date$month, levels=1:12, labels=c(0.22,0.22,0.22,0.29,0.29,0.28,0.26,0.25,0.23,0.22,0.22,0.22))

However, your values seem better suited to a numeric variable rather than a factor (in R, 'factor' refers to something categorical). If that is in fact true, you could do:

values = c(0.22,0.22,0.22,0.29,0.29,0.28,0.26,0.25,0.23,0.22,0.22,0.22)
Haude.input$monthF <- values[df.date$month]

Altri suggerimenti

Sounds like you want the month stored as a separate variable? If you create the date object, you can format it as anything you like.

temp$date =as.Date(temp$date,format="%Y-%m-%d")
temp$month =format(temp$date, format="%B")
temp$month2 =format(temp$date, format="%m")

result:

> temp$month
[1] "May" "May" "May" "May" "May"

Or, as a number:

> temp$month2
[1] "05" "05" "05" "05" "05"

EDIT: it sounds like you want to look up the value from the factor list (probably call it something else, that term is already used in the R world). Then you can use the month value, as @JVL suggests, to look up, but you need to convert to an integer first. May will be 5, June will be seven and so forth.

 value_factor=c(0.22,0.22,0.22,0.29,0.29,0.28,0.26,0.25,0.23,0.22,0.22,0.22)
 value_factor[as.numeric(temp$month)]

For your data, it becomes

 f[as.numeric(temp$month)]
 [1] 0.29 0.29 0.29 0.29 0.29
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top