R将数据框列的所有行除以一个数字
R Divide all rows of a dataframe column by a number
我正在尝试将数据框列的所有行除以一个数字(比如 10)。在我尝试之前,我认为这是一个微不足道的问题。在下面的示例中,我试图让 'mm' 列产生值 8100、3222.2 和 5433.3
test <- data.frame(locations=c("81000","32222","54333"), value=c(87,54,43))
test$mm <- as.numeric(test$locations) / 10
head(test)
locations value mm
1 81000 87 0.3
2 32222 54 0.1
3 54333 43 0.2
我做错了什么?
将 factors
更改为 character
,然后应用 as.numeric
> test$mm <- as.numeric(as.character(test$locations)) / 10
> test
locations value mm
1 81000 87 8100.0
2 32222 54 3222.2
3 54333 43 5433.3
我正在尝试将数据框列的所有行除以一个数字(比如 10)。在我尝试之前,我认为这是一个微不足道的问题。在下面的示例中,我试图让 'mm' 列产生值 8100、3222.2 和 5433.3
test <- data.frame(locations=c("81000","32222","54333"), value=c(87,54,43))
test$mm <- as.numeric(test$locations) / 10
head(test)
locations value mm
1 81000 87 0.3
2 32222 54 0.1
3 54333 43 0.2
我做错了什么?
将 factors
更改为 character
,然后应用 as.numeric
> test$mm <- as.numeric(as.character(test$locations)) / 10
> test
locations value mm
1 81000 87 8100.0
2 32222 54 3222.2
3 54333 43 5433.3