0

I'm new with programming and try to work with R for the first time. I'm actually trying to set some points into a plot and fail to do the easiest things...

First of all: How can i use a function like:

coordx<-function(final_distance,radiant){
  x<-(sin(radiant)*final_distance)
  return(x)


xfinal<-coordx(dat$final_distance,dat$radiant)
dat$xfinal<-xfinal
}    

without some values in my dataframe dat.

I have in the 2nd column radiant a few values which are wrong and need to be ignored, but I don't like to delete them, for statistical reasons.

Second question: Even if I used it before; whats the easiest way to make a new column out of a functions results?

Im sry for asking such basic and moronic questions... this a "first-try" in programming... :)

cheers, steveo

user1317221_G
  • 15,087
  • 3
  • 52
  • 78
user2999399
  • 23
  • 2
  • 6
  • 3
    [How to make a great R reproducible example?](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – zero323 Nov 16 '13 at 13:51

1 Answers1

1

Here is a detailed answer

# Get 10 numbers for final_distance column using sample()
final_distance = sample(1:10)

# Get 10 numbers for radiant column using sample()
    radiant = sample(11:20)

# Create a data frame using the columns final_distance and radiant
    df <- data.frame(final_distance, radiant)

# Write a function 
    coordx<-function(x, y){
      new <-(sin(y) * x)
      return(new)
    }

# Filter the unwanted values in the radiant column
I considered to ignore values that are multiples of 3 (You can write your own criteria)
    df.filtered <- df[df$radiant%%3 != 0,]

# Add new column 'newcol' to data frame 'df' using the above defined function coordx
    df$newcol <- coordx(df.filtered$final_distance, df.filtered$radiant)

# Print the df finally (along with the new column).
    df

As per your recent comment, I am adding the second piece of code with radiant as a colu,m with string values.

# Get 10 numbers for final_distance column using sample()
final_distance = sample(1:10)

# Get 10 strings for radiant column using sample()
radiant = sample(11:20)

# Another column besides final_distance and radiant that has only strings
Other_column = sample(c("one", "two", "three", "four"), 10, replace = TRUE)

# Create a data frame using the columns final_distance and radiant
df <- data.frame(final_distance, radiant, other_column)

> str(df)
'data.frame':   10 obs. of  3 variables:
 $ final_distance: int  4 3 8 7 5 6 10 1 9 2
 $ radiant       : int  13 16 12 11 19 14 15 18 20 17
 $ other_column  : Factor w/ 4 levels "four","one","three",..: 2 3 2 4 3 2 4 2 1 4

# Write a function 
coordx<-function(x, y){
  new <-(sin(y) * x)
  return(new)
}

# Filter the unwanted values in the radiant column
# I considered to ignore values that are multiples of 3 (You can write your own criteria)
df.filtered <- df[!(df$other_column %in% c("one", "three")), ]

# Add new column 'newcol' to data frame 'df' using the above defined function coordx
df.filtered$newcol <- coordx(df.filtered$final_distance, df.filtered$radiant)

# Print the df finally (along with the new column).
df.filtered

Hope, It helps !!

Kumar
  • 314
  • 3
  • 5
  • 16
  • well, i think that its a great idea, but i have to filter non-numeric values, which are in the column, like x1,x2,x3.... if i dont wanna see x2,x17,x29... how can i do it? – user2999399 Nov 16 '13 at 16:39
  • Refer the second part of the code added in my above response. – Kumar Nov 17 '13 at 04:54