我是编程的新手,并且第一次尝试使用R。 我实际上是想把一些点放到一个情节中而不能做最简单的事情......
首先:我如何使用如下函数:
coordx<-function(final_distance,radiant){
x<-(sin(radiant)*final_distance)
return(x)
xfinal<-coordx(dat$final_distance,dat$radiant)
dat$xfinal<-xfinal
}
我的dataframe
dat
中没有一些值。
我在第二列radiant
中有一些错误的值,需要忽略,但出于统计原因,我不想删除它们。
第二个问题:即使我之前使用过它;什么是从函数结果中创建新列的最简单方法?
我想问这些基本和愚蠢的问题......这是编程中的“第一次尝试”......)
欢呼,史蒂夫答案 0 :(得分:1)
这是一个详细的答案
# 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
根据你最近的评论,我正在添加第二段代码,其中radiant为colu,m为字符串值。
# 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
希望,它有所帮助!!