To whom it may concern, I am a new user to R and I need help in my if and for statements as I need a place marker. I would like to convert the following table: Observation | Variable 1 --------------------|------------- 1 | 10000 2 | A 3 | 631 Into : Observation| Variable 1 | Variable 1 Flag ----------------|------------------|------------------ 1 | 10000 | NA 2 | NA | A 3 | 631 | NA please note | represents another column. this is for a large data set and I cannot manually go through this: my attempt to this was : l=0 # placeholder m<-c() #newvariable 1 v<-c() #newvariable flag for (z in tablename1$Variable_1) (if (is.numeric(z) ) m[z:l] l=l+1 else v[z:1] l=l+1 Can you please guide me as to how to approach this problem? Any help would be greatly appreciated. Tarun Manchanda [[alternative HTML version deleted]]
Hello,
three commands might do the job (NOTE: df=your data frame,
obser=Observation, var1=Variable 1 [TYPE: string], var1flag=Variable 1
flag [TYPE: string])
1. df$var1flag<-NA
2. df$var1flag[ is.na(as.numeric(df$var1)) ]<-df$var1[
is.na(as.numeric(df$var1)) ]
3. df$var1<-as.numeric(df$var1)
{Please note: Var1 must be type string at the beginning. Otherwise this
would not work (you cannot have an "A" in a numeric column...)]
After this, 'df' gives:
> df
obser var1 var1flag
1 1 1000 <NA>
2 2 <NA> A
3 3 631 <NA>
Is this what You are looking for?
HTH,
Kimmo
02.06.2011 18:28, Tarun Manchanda wrote:> To whom it may concern,
> I am a new user to R and I need help in my if and for statements as I need
a place marker.
>
> I would like to convert the following table:
> Observation | Variable 1
> --------------------|-------------
> 1 | 10000
> 2 | A
> 3 | 631
>
> Into :
> Observation| Variable 1 | Variable 1 Flag
> ----------------|------------------|------------------
> 1 | 10000 | NA
> 2 | NA | A
> 3 | 631 | NA
>
>
> please note | represents another column.
Seemingly Similar Threads
- [LLVMdev] bug in ARMFrameLowering.cpp:processFunctionBeforeCalleeSavedScan
- Applying function to a TABLE and also "apply, tapply, sapply etc"
- Multiple logical operations in a subscript
- New Variable from Several Existing Variables
- Fortran routine affected by unrelated R code